| 1 | /*
|
|---|
| 2 | * Copyright (c) 2010 Stanislav Kozina
|
|---|
| 3 | * All rights reserved.
|
|---|
| 4 | *
|
|---|
| 5 | * Redistribution and use in source and binary forms, with or without
|
|---|
| 6 | * modification, are permitted provided that the following conditions
|
|---|
| 7 | * are met:
|
|---|
| 8 | *
|
|---|
| 9 | * - Redistributions of source code must retain the above copyright
|
|---|
| 10 | * notice, this list of conditions and the following disclaimer.
|
|---|
| 11 | * - Redistributions in binary form must reproduce the above copyright
|
|---|
| 12 | * notice, this list of conditions and the following disclaimer in the
|
|---|
| 13 | * documentation and/or other materials provided with the distribution.
|
|---|
| 14 | * - The name of the author may not be used to endorse or promote products
|
|---|
| 15 | * derived from this software without specific prior written permission.
|
|---|
| 16 | *
|
|---|
| 17 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
|---|
| 18 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
|---|
| 19 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
|---|
| 20 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
|---|
| 21 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
|---|
| 22 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
|---|
| 23 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
|---|
| 24 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
|---|
| 25 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
|---|
| 26 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|---|
| 27 | */
|
|---|
| 28 |
|
|---|
| 29 | /** @addtogroup ps
|
|---|
| 30 | * @brief Task lister.
|
|---|
| 31 | * @{
|
|---|
| 32 | */
|
|---|
| 33 | /**
|
|---|
| 34 | * @file
|
|---|
| 35 | */
|
|---|
| 36 |
|
|---|
| 37 | #include <stdio.h>
|
|---|
| 38 | #include <task.h>
|
|---|
| 39 | #include <thread.h>
|
|---|
| 40 | #include <ps.h>
|
|---|
| 41 | #include <errno.h>
|
|---|
| 42 | #include <stdlib.h>
|
|---|
| 43 | #include <malloc.h>
|
|---|
| 44 | #include <load.h>
|
|---|
| 45 | #include <sysinfo.h>
|
|---|
| 46 |
|
|---|
| 47 | #include "func.h"
|
|---|
| 48 |
|
|---|
| 49 | #define TASK_COUNT 10
|
|---|
| 50 | #define THREAD_COUNT 50
|
|---|
| 51 |
|
|---|
| 52 | #define ECHOLOAD1(x) ((x) >> 11)
|
|---|
| 53 | #define ECHOLOAD2(x) (((x) & 0x7ff) / 2)
|
|---|
| 54 |
|
|---|
| 55 | /** Thread states */
|
|---|
| 56 | static const char *thread_states[] = {
|
|---|
| 57 | "Invalid",
|
|---|
| 58 | "Running",
|
|---|
| 59 | "Sleeping",
|
|---|
| 60 | "Ready",
|
|---|
| 61 | "Entering",
|
|---|
| 62 | "Exiting",
|
|---|
| 63 | "Lingering"
|
|---|
| 64 | };
|
|---|
| 65 |
|
|---|
| 66 | static void list_tasks(void)
|
|---|
| 67 | {
|
|---|
| 68 | int task_count = TASK_COUNT;
|
|---|
| 69 | task_id_t *tasks = malloc(task_count * sizeof(task_id_t));
|
|---|
| 70 | int result = get_task_ids(tasks, sizeof(task_id_t) * task_count);
|
|---|
| 71 |
|
|---|
| 72 | while (result > task_count) {
|
|---|
| 73 | task_count *= 2;
|
|---|
| 74 | tasks = realloc(tasks, task_count * sizeof(task_id_t));
|
|---|
| 75 | result = get_task_ids(tasks, sizeof(task_id_t) * task_count);
|
|---|
| 76 | }
|
|---|
| 77 |
|
|---|
| 78 | printf(" ID Threads Mem uCycles kCycles Cycle fault Name\n");
|
|---|
| 79 |
|
|---|
| 80 | int i;
|
|---|
| 81 | for (i = 0; i < result; ++i) {
|
|---|
| 82 | task_info_t taskinfo;
|
|---|
| 83 | get_task_info(tasks[i], &taskinfo);
|
|---|
| 84 | uint64_t mem, ucycles, kcycles;
|
|---|
| 85 | char memsuffix, usuffix, ksuffix;
|
|---|
| 86 | order(taskinfo.virt_mem, &mem, &memsuffix);
|
|---|
| 87 | order(taskinfo.ucycles, &ucycles, &usuffix);
|
|---|
| 88 | order(taskinfo.kcycles, &kcycles, &ksuffix);
|
|---|
| 89 | printf("%8llu %8u %8llu%c %12llu%c %12llu%c %s\n", tasks[i],
|
|---|
| 90 | taskinfo.thread_count, mem, memsuffix, ucycles, usuffix,
|
|---|
| 91 | kcycles, ksuffix, taskinfo.name);
|
|---|
| 92 | }
|
|---|
| 93 |
|
|---|
| 94 | free(tasks);
|
|---|
| 95 | }
|
|---|
| 96 |
|
|---|
| 97 | static void list_threads(task_id_t taskid)
|
|---|
| 98 | {
|
|---|
| 99 | int thread_count = THREAD_COUNT;
|
|---|
| 100 | thread_info_t *threads = malloc(thread_count * sizeof(thread_info_t));
|
|---|
| 101 | int result = get_task_threads(taskid, threads, sizeof(thread_info_t) * thread_count);
|
|---|
| 102 |
|
|---|
| 103 | while (result > thread_count) {
|
|---|
| 104 | thread_count *= 2;
|
|---|
| 105 | threads = realloc(threads, thread_count * sizeof(thread_info_t));
|
|---|
| 106 | result = get_task_threads(taskid, threads, sizeof(thread_info_t) * thread_count);
|
|---|
| 107 | }
|
|---|
| 108 |
|
|---|
| 109 | if (result == 0) {
|
|---|
| 110 | printf("No task with given pid!\n");
|
|---|
| 111 | exit(1);
|
|---|
| 112 | }
|
|---|
| 113 |
|
|---|
| 114 | int i;
|
|---|
| 115 | printf(" ID State CPU Prio [k]uCycles [k]kcycles Cycle fault\n");
|
|---|
| 116 | for (i = 0; i < result; ++i) {
|
|---|
| 117 | uint64_t ucycles, kcycles;
|
|---|
| 118 | char usuffix, ksuffix;
|
|---|
| 119 | order(threads[i].ucycles, &ucycles, &usuffix);
|
|---|
| 120 | order(threads[i].kcycles, &kcycles, &ksuffix);
|
|---|
| 121 | printf("%6llu %-8s %4u %6d %12llu%c %12llu%c\n", threads[i].tid,
|
|---|
| 122 | thread_states[threads[i].state], threads[i].cpu,
|
|---|
| 123 | threads[i].priority, ucycles, usuffix,
|
|---|
| 124 | kcycles, ksuffix);
|
|---|
| 125 | }
|
|---|
| 126 |
|
|---|
| 127 | free(threads);
|
|---|
| 128 | }
|
|---|
| 129 |
|
|---|
| 130 | static void echo_load(void)
|
|---|
| 131 | {
|
|---|
| 132 | unsigned long load[3];
|
|---|
| 133 | get_load(load);
|
|---|
| 134 | printf("load avarage: ");
|
|---|
| 135 | print_load_fragment(load[0], 2);
|
|---|
| 136 | puts(" ");
|
|---|
| 137 | print_load_fragment(load[1], 2);
|
|---|
| 138 | puts(" ");
|
|---|
| 139 | print_load_fragment(load[2], 2);
|
|---|
| 140 | puts("\n");
|
|---|
| 141 | }
|
|---|
| 142 |
|
|---|
| 143 | static void echo_cpus(void)
|
|---|
| 144 | {
|
|---|
| 145 | size_t cpu_count = sysinfo_value("cpu.count");
|
|---|
| 146 | printf("Found %u cpu's:\n", cpu_count);
|
|---|
| 147 | uspace_cpu_info_t *cpus = malloc(cpu_count * sizeof(uspace_cpu_info_t));
|
|---|
| 148 | get_cpu_info(cpus);
|
|---|
| 149 | size_t i;
|
|---|
| 150 | for (i = 0; i < cpu_count; ++i) {
|
|---|
| 151 | printf("%2u (%4u Mhz): Busy ticks: %6llu, Idle ticks: %6llu\n", cpus[i].id,
|
|---|
| 152 | (size_t)cpus[i].frequency_mhz, cpus[i].busy_ticks, cpus[i].idle_ticks);
|
|---|
| 153 | }
|
|---|
| 154 | }
|
|---|
| 155 |
|
|---|
| 156 | static void usage()
|
|---|
| 157 | {
|
|---|
| 158 | printf("Usage: ps [-t pid|-l|-c]\n");
|
|---|
| 159 | }
|
|---|
| 160 |
|
|---|
| 161 | int main(int argc, char *argv[])
|
|---|
| 162 | {
|
|---|
| 163 | --argc; ++argv;
|
|---|
| 164 |
|
|---|
| 165 | if (argc > 0)
|
|---|
| 166 | {
|
|---|
| 167 | if (str_cmp(*argv, "-t") == 0) {
|
|---|
| 168 | --argc; ++argv;
|
|---|
| 169 | if (argc != 1) {
|
|---|
| 170 | printf("Bad argument count!\n");
|
|---|
| 171 | usage();
|
|---|
| 172 | exit(1);
|
|---|
| 173 | }
|
|---|
| 174 | task_id_t taskid = strtol(*argv, NULL, 10);
|
|---|
| 175 | list_threads(taskid);
|
|---|
| 176 | } else if (str_cmp(*argv, "-l") == 0) {
|
|---|
| 177 | --argc; ++argv;
|
|---|
| 178 | if (argc != 0) {
|
|---|
| 179 | printf("Bad argument count!\n");
|
|---|
| 180 | usage();
|
|---|
| 181 | exit(1);
|
|---|
| 182 | }
|
|---|
| 183 | echo_load();
|
|---|
| 184 | } else if (str_cmp(*argv, "-c") == 0) {
|
|---|
| 185 | --argc; ++argv;
|
|---|
| 186 | if (argc != 0) {
|
|---|
| 187 | printf("Bad argument count!\n");
|
|---|
| 188 | usage();
|
|---|
| 189 | exit(1);
|
|---|
| 190 | }
|
|---|
| 191 | echo_cpus();
|
|---|
| 192 | } else {
|
|---|
| 193 | printf("Unknown argument %s!\n", *argv);
|
|---|
| 194 | usage();
|
|---|
| 195 | exit(1);
|
|---|
| 196 | }
|
|---|
| 197 | } else {
|
|---|
| 198 | list_tasks();
|
|---|
| 199 | }
|
|---|
| 200 |
|
|---|
| 201 | return 0;
|
|---|
| 202 | }
|
|---|
| 203 |
|
|---|
| 204 | /** @}
|
|---|
| 205 | */
|
|---|