00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00038 #include <stdio.h>
00039 #include <task.h>
00040 #include <thread.h>
00041 #include <stats.h>
00042 #include <errno.h>
00043 #include <stdlib.h>
00044 #include <malloc.h>
00045 #include <inttypes.h>
00046 #include <bool.h>
00047 #include <str.h>
00048 #include <arg_parse.h>
00049
00050 #define NAME "stats"
00051
00052 #define DAY 86400
00053 #define HOUR 3600
00054 #define MINUTE 60
00055
00056 static void list_tasks(void)
00057 {
00058 size_t count;
00059 stats_task_t *stats_tasks = stats_get_tasks(&count);
00060
00061 if (stats_tasks == NULL) {
00062 fprintf(stderr, "%s: Unable to get tasks\n", NAME);
00063 return;
00064 }
00065
00066 printf("[taskid] [thrds] [resident] [virtual] [ucycles]"
00067 " [kcycles] [name\n");
00068
00069 size_t i;
00070 for (i = 0; i < count; i++) {
00071 uint64_t resmem;
00072 uint64_t virtmem;
00073 uint64_t ucycles;
00074 uint64_t kcycles;
00075 const char *resmem_suffix;
00076 const char *virtmem_suffix;
00077 char usuffix;
00078 char ksuffix;
00079
00080 bin_order_suffix(stats_tasks[i].resmem, &resmem, &resmem_suffix, true);
00081 bin_order_suffix(stats_tasks[i].virtmem, &virtmem, &virtmem_suffix, true);
00082 order_suffix(stats_tasks[i].ucycles, &ucycles, &usuffix);
00083 order_suffix(stats_tasks[i].kcycles, &kcycles, &ksuffix);
00084
00085 printf("%-8" PRIu64 " %7zu %7" PRIu64 "%s %6" PRIu64 "%s"
00086 " %8" PRIu64 "%c %8" PRIu64 "%c %s\n",
00087 stats_tasks[i].task_id, stats_tasks[i].threads,
00088 resmem, resmem_suffix, virtmem, virtmem_suffix,
00089 ucycles, usuffix, kcycles, ksuffix, stats_tasks[i].name);
00090 }
00091
00092 free(stats_tasks);
00093 }
00094
00095 static void list_threads(task_id_t task_id, bool all)
00096 {
00097 size_t count;
00098 stats_thread_t *stats_threads = stats_get_threads(&count);
00099
00100 if (stats_threads == NULL) {
00101 fprintf(stderr, "%s: Unable to get threads\n", NAME);
00102 return;
00103 }
00104
00105 printf("[taskid] [threadid] [state ] [prio] [cpu ] [ucycles] [kcycles]\n");
00106
00107 size_t i;
00108 for (i = 0; i < count; i++) {
00109 if ((all) || (stats_threads[i].task_id == task_id)) {
00110 uint64_t ucycles, kcycles;
00111 char usuffix, ksuffix;
00112
00113 order_suffix(stats_threads[i].ucycles, &ucycles, &usuffix);
00114 order_suffix(stats_threads[i].kcycles, &kcycles, &ksuffix);
00115
00116 printf("%-8" PRIu64 " %-10" PRIu64 " %-8s %6d ",
00117 stats_threads[i].task_id, stats_threads[i].thread_id,
00118 thread_get_state(stats_threads[i].state),
00119 stats_threads[i].priority);
00120
00121 if (stats_threads[i].on_cpu)
00122 printf("%6u ", stats_threads[i].cpu);
00123 else
00124 printf("(none) ");
00125
00126 printf("%8" PRIu64"%c %8" PRIu64"%c\n",
00127 ucycles, usuffix, kcycles, ksuffix);
00128 }
00129 }
00130
00131 free(stats_threads);
00132 }
00133
00134 static void list_cpus(void)
00135 {
00136 size_t count;
00137 stats_cpu_t *cpus = stats_get_cpus(&count);
00138
00139 if (cpus == NULL) {
00140 fprintf(stderr, "%s: Unable to get CPU statistics\n", NAME);
00141 return;
00142 }
00143
00144 printf("[id] [MHz ] [busy cycles] [idle cycles]\n");
00145
00146 size_t i;
00147 for (i = 0; i < count; i++) {
00148 printf("%-4u ", cpus[i].id);
00149 if (cpus[i].active) {
00150 uint64_t bcycles, icycles;
00151 char bsuffix, isuffix;
00152
00153 order_suffix(cpus[i].busy_cycles, &bcycles, &bsuffix);
00154 order_suffix(cpus[i].idle_cycles, &icycles, &isuffix);
00155
00156 printf("%10" PRIu16 " %12" PRIu64 "%c %12" PRIu64 "%c\n",
00157 cpus[i].frequency_mhz, bcycles, bsuffix,
00158 icycles, isuffix);
00159 } else
00160 printf("inactive\n");
00161 }
00162
00163 free(cpus);
00164 }
00165
00166 static void print_load(void)
00167 {
00168 size_t count;
00169 load_t *load = stats_get_load(&count);
00170
00171 if (load == NULL) {
00172 fprintf(stderr, "%s: Unable to get load\n", NAME);
00173 return;
00174 }
00175
00176 printf("%s: Load average: ", NAME);
00177
00178 size_t i;
00179 for (i = 0; i < count; i++) {
00180 if (i > 0)
00181 printf(" ");
00182
00183 stats_print_load_fragment(load[i], 2);
00184 }
00185
00186 printf("\n");
00187
00188 free(load);
00189 }
00190
00191 static void print_uptime(void)
00192 {
00193 sysarg_t uptime = stats_get_uptime();
00194 printf("%s: Up %" PRIun " days, %" PRIun " hours, "
00195 "%" PRIun " minutes, %" PRIun " seconds\n", NAME,
00196 uptime / DAY, (uptime % DAY) / HOUR,
00197 (uptime % HOUR) / MINUTE, uptime % MINUTE);
00198 }
00199
00200 static void usage(const char *name)
00201 {
00202 printf(
00203 "Usage: %s [-t task_id] [-a] [-c] [-l] [-u]\n" \
00204 "\n" \
00205 "Options:\n" \
00206 "\t-t task_id\n" \
00207 "\t--task=task_id\n" \
00208 "\t\tList threads of the given task\n" \
00209 "\n" \
00210 "\t-a\n" \
00211 "\t--all\n" \
00212 "\t\tList all threads\n" \
00213 "\n" \
00214 "\t-c\n" \
00215 "\t--cpus\n" \
00216 "\t\tList CPUs\n" \
00217 "\n" \
00218 "\t-l\n" \
00219 "\t--load\n" \
00220 "\t\tPrint system load\n" \
00221 "\n" \
00222 "\t-u\n" \
00223 "\t--uptime\n" \
00224 "\t\tPrint system uptime\n" \
00225 "\n" \
00226 "\t-h\n" \
00227 "\t--help\n" \
00228 "\t\tPrint this usage information\n"
00229 "\n" \
00230 "Without any options all tasks are listed\n",
00231 name
00232 );
00233 }
00234
00235 int main(int argc, char *argv[])
00236 {
00237 bool toggle_tasks = true;
00238 bool toggle_threads = false;
00239 bool toggle_all = false;
00240 bool toggle_cpus = false;
00241 bool toggle_load = false;
00242 bool toggle_uptime = false;
00243
00244 task_id_t task_id = 0;
00245
00246 int i;
00247 for (i = 1; i < argc; i++) {
00248 int off;
00249
00250
00251 if ((off = arg_parse_short_long(argv[i], "-h", "--help")) != -1) {
00252 usage(argv[0]);
00253 return 0;
00254 }
00255
00256
00257 if ((off = arg_parse_short_long(argv[i], "-a", "--all")) != -1) {
00258 toggle_tasks = false;
00259 toggle_threads = true;
00260 toggle_all = true;
00261 continue;
00262 }
00263
00264
00265 if ((off = arg_parse_short_long(argv[i], "-c", "--cpus")) != -1) {
00266 toggle_tasks = false;
00267 toggle_cpus = true;
00268 continue;
00269 }
00270
00271
00272 if ((off = arg_parse_short_long(argv[i], "-t", "--task=")) != -1) {
00273
00274 int tmp;
00275 int ret = arg_parse_int(argc, argv, &i, &tmp, off);
00276 if (ret != EOK) {
00277 printf("%s: Malformed task_id '%s'\n", NAME, argv[i]);
00278 return -1;
00279 }
00280
00281 task_id = tmp;
00282
00283 toggle_tasks = false;
00284 toggle_threads = true;
00285 continue;
00286 }
00287
00288
00289 if ((off = arg_parse_short_long(argv[i], "-l", "--load")) != -1) {
00290 toggle_tasks = false;
00291 toggle_load = true;
00292 continue;
00293 }
00294
00295
00296 if ((off = arg_parse_short_long(argv[i], "-u", "--uptime")) != -1) {
00297 toggle_tasks = false;
00298 toggle_uptime = true;
00299 continue;
00300 }
00301 }
00302
00303 if (toggle_tasks)
00304 list_tasks();
00305
00306 if (toggle_threads)
00307 list_threads(task_id, toggle_all);
00308
00309 if (toggle_cpus)
00310 list_cpus();
00311
00312 if (toggle_load)
00313 print_load();
00314
00315 if (toggle_uptime)
00316 print_uptime();
00317
00318 return 0;
00319 }
00320