Changeset 9dae191e in mainline for uspace/app/ps/ps.c


Ignore:
Timestamp:
2010-04-18T00:24:40Z (14 years ago)
Author:
Martin Decky <martin@…>
Branches:
lfn, master, serial, ticket/834-toolchain-update, topic/msim-upgrade, topic/simplify-dev-export
Children:
a80687e5
Parents:
d8e3467
Message:

sysinfo API cleanup

  • better support for generated subtrees
  • synchronization
  • memory management (generated items cleanup)
  • simplier sysinfo_dump()

remove separate statistical syscalls, replace with virtual sysinfo items (some functionality is still missing)

  • naming consolidation
  • cleaner API
  • proper synchronization

minor renames

  • zone_print_list() → zones_print_list()
  • zone_busy_and_free() → zones_stats()
File:
1 edited

Legend:

Unmodified
Added
Removed
  • uspace/app/ps/ps.c

    rd8e3467 r9dae191e  
    3838#include <task.h>
    3939#include <thread.h>
    40 #include <ps.h>
     40#include <stats.h>
    4141#include <errno.h>
    4242#include <stdlib.h>
    4343#include <malloc.h>
    44 #include <load.h>
    45 #include <sysinfo.h>
    46 
     44#include <inttypes.h>
     45#include <arg_parse.h>
    4746#include "func.h"
    4847
    49 #define TASK_COUNT 10
    50 #define THREAD_COUNT 50
    51 
    52 #define ECHOLOAD1(x) ((x) >> 11)
    53 #define ECHOLOAD2(x) (((x) & 0x7ff) / 2)
     48#define NAME  "ps"
     49
     50#define TASK_COUNT    10
     51#define THREAD_COUNT  50
     52
     53#define PRINT_LOAD1(x)  ((x) >> 11)
     54#define PRINT_LOAD2(x)  (((x) & 0x7ff) / 2)
    5455
    5556/** Thread states */
    56 static const char *thread_states[] = {
    57         "Invalid",
    58         "Running",
    59         "Sleeping",
    60         "Ready",
    61         "Entering",
    62         "Exiting",
    63         "Lingering"
    64 };
     57//static const char *thread_states[] = {
     58//      "Invalid",
     59//      "Running",
     60//      "Sleeping",
     61//      "Ready",
     62//      "Entering",
     63//      "Exiting",
     64//      "Lingering"
     65//};
    6566
    6667static void list_tasks(void)
    6768{
    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 {
     69        size_t count;
     70        task_id_t *ids =
     71            (task_id_t *) get_stats_tasks(&count);
     72       
     73        if (ids == NULL) {
     74                fprintf(stderr, "%s: Unable to get tasks\n", NAME);
     75                return;
     76        }
     77       
     78        printf("      ID  Threads      Mem       uCycles       kCycles   Name\n");
     79       
     80        size_t i;
     81        for (i = 0; i < count; i++) {
     82                stats_task_t *stats_task = get_stats_task(ids[i]);
     83                if (stats_task != NULL) {
     84                        uint64_t virtmem, ucycles, kcycles;
     85                        char vmsuffix, usuffix, ksuffix;
     86                       
     87                        order(stats_task->virtmem, &virtmem, &vmsuffix);
     88                        order(stats_task->ucycles, &ucycles, &usuffix);
     89                        order(stats_task->kcycles, &kcycles, &ksuffix);
     90                       
     91                        printf("%8" PRIu64 "%8u %8" PRIu64"%c %12"
     92                            PRIu64"%c %12" PRIu64"%c %s\n", ids[i], stats_task->threads,
     93                            virtmem, vmsuffix, ucycles, usuffix, kcycles, ksuffix,
     94                            stats_task->name);
     95                       
     96                        free(stats_task);
     97                } else
     98                        printf("%8" PRIu64 "\n", ids[i]);
     99        }
     100       
     101        free(ids);
     102}
     103
     104static void list_threads(task_id_t task_id)
     105{
     106        /* TODO:
    99107        size_t thread_count = THREAD_COUNT;
    100108        thread_info_t *threads = malloc(thread_count * sizeof(thread_info_t));
     
    128136        }
    129137
    130         free(threads);
    131 }
    132 
    133 static void echo_load(void)
    134 {
    135         unsigned long load[3];
    136         get_load(load);
    137         printf("load avarage: ");
    138         print_load_fragment(load[0], 2);
    139         puts(" ");
    140         print_load_fragment(load[1], 2);
    141         puts(" ");
    142         print_load_fragment(load[2], 2);
    143         puts("\n");
    144 }
    145 
    146 static void echo_cpus(void)
    147 {
    148         size_t cpu_count = sysinfo_value("cpu.count");
    149         printf("Found %u cpu's:\n", cpu_count);
    150         uspace_cpu_info_t *cpus = malloc(cpu_count * sizeof(uspace_cpu_info_t));
    151         get_cpu_info(cpus);
    152         size_t i;
    153         for (i = 0; i < cpu_count; ++i) {
    154                 printf("%2u (%4u Mhz): Busy ticks: %6llu, Idle ticks: %6llu\n", cpus[i].id,
    155                                 (size_t)cpus[i].frequency_mhz, cpus[i].busy_ticks, cpus[i].idle_ticks);
    156         }
     138        free(threads); */
     139}
     140
     141static void print_load(void)
     142{
     143        size_t count;
     144        load_t *load = get_stats_load(&count);
     145       
     146        if (load == NULL) {
     147                fprintf(stderr, "%s: Unable to get load\n", NAME);
     148                return;
     149        }
     150       
     151        printf("%s: Load avarage: ", NAME);
     152       
     153        size_t i;
     154        for (i = 0; i < count; i++) {
     155                if (i > 0)
     156                        printf(" ");
     157               
     158                print_load_fragment(load[i], 2);
     159        }
     160       
     161        printf("\n");
     162       
     163        free(load);
     164}
     165
     166static void list_cpus(void)
     167{
     168        size_t count;
     169        stats_cpu_t *cpus = get_stats_cpus(&count);
     170       
     171        if (cpus == NULL) {
     172                fprintf(stderr, "%s: Unable to get CPU statistics\n", NAME);
     173                return;
     174        }
     175       
     176        printf("%s: %u CPU(s) detected\n", NAME, count);
     177       
     178        size_t i;
     179        for (i = 0; i < count; i++) {
     180                printf("cpu%u: %" PRIu16 " MHz, busy ticks: "
     181                    "%" PRIu64 ", idle ticks: %" PRIu64 "\n",
     182                    cpus[i].id, cpus[i].frequency_mhz, cpus[i].busy_ticks,
     183                    cpus[i].idle_ticks);
     184        }
     185       
     186        free(cpus);
    157187}
    158188
    159189static void usage()
    160190{
    161         printf("Usage: ps [-t pid|-l|-c]\n");
     191        printf(
     192            "Usage: ps [-t task_id] [-l] [-c]\n" \
     193            "\n" \
     194            "Options:\n" \
     195            "\t-t task_id\n" \
     196            "\t--task=task_id\n" \
     197            "\t\tList threads of the given task\n" \
     198            "\n" \
     199            "\t-l\n" \
     200            "\t--load\n" \
     201            "\t\tPrint system load\n" \
     202            "\n" \
     203            "\t-c\n" \
     204            "\t--cpu\n" \
     205            "\t\tList CPUs\n" \
     206            "\n" \
     207            "\t-h\n" \
     208            "\t--help\n" \
     209            "\t\tPrint this usage information\n"
     210            "\n" \
     211            "Without any options all tasks are listed\n"
     212        );
    162213}
    163214
    164215int main(int argc, char *argv[])
    165216{
    166         --argc; ++argv;
    167 
    168         if (argc > 0)
    169         {
    170                 if (str_cmp(*argv, "-t") == 0) {
    171                         --argc; ++argv;
    172                         if (argc != 1) {
    173                                 printf("Bad argument count!\n");
    174                                 usage();
    175                                 exit(1);
     217        bool toggle_tasks = true;
     218        bool toggle_threads = false;
     219        bool toggle_load = false;
     220        bool toggle_cpus = false;
     221       
     222        task_id_t task_id;
     223       
     224        int i;
     225        for (i = 1; i < argc; i++) {
     226                int off;
     227               
     228                /* Usage */
     229                if ((off = arg_parse_short_long(argv[i], "-h", "--help")) != -1) {
     230                        usage();
     231                        return 0;
     232                }
     233               
     234                /* Load */
     235                if ((off = arg_parse_short_long(argv[i], "-l", "--load")) != -1) {
     236                        toggle_tasks = false;
     237                        toggle_load = true;
     238                        continue;
     239                }
     240               
     241                /* CPUs */
     242                if ((off = arg_parse_short_long(argv[i], "-c", "--cpus")) != -1) {
     243                        toggle_tasks = false;
     244                        toggle_cpus = true;
     245                        continue;
     246                }
     247               
     248                /* Threads */
     249                if ((off = arg_parse_short_long(argv[i], "-t", "--task=")) != -1) {
     250                        // TODO: Support for 64b range
     251                        int tmp;
     252                        int ret = arg_parse_int(argc, argv, &i, &tmp, off);
     253                        if (ret != EOK) {
     254                                printf("%s: Malformed task_id '%s'\n", NAME, argv[i]);
     255                                return -1;
    176256                        }
    177                         task_id_t taskid = strtol(*argv, NULL, 10);
    178                         list_threads(taskid);
    179                 } else if (str_cmp(*argv, "-l") == 0) {
    180                         --argc; ++argv;
    181                         if (argc != 0) {
    182                                 printf("Bad argument count!\n");
    183                                 usage();
    184                                 exit(1);
    185                         }
    186                         echo_load();
    187                 } else if (str_cmp(*argv, "-c") == 0) {
    188                         --argc; ++argv;
    189                         if (argc != 0) {
    190                                 printf("Bad argument count!\n");
    191                                 usage();
    192                                 exit(1);
    193                         }
    194                         echo_cpus();
    195                 } else {
    196                         printf("Unknown argument %s!\n", *argv);
    197                         usage();
    198                         exit(1);
    199                 }
    200         } else {
     257                       
     258                        task_id = tmp;
     259                       
     260                        toggle_tasks = false;
     261                        toggle_threads = true;
     262                        continue;
     263                }
     264        }
     265       
     266        if (toggle_tasks)
    201267                list_tasks();
    202         }
    203 
     268       
     269        if (toggle_threads)
     270                list_threads(task_id);
     271       
     272        if (toggle_load)
     273                print_load();
     274       
     275        if (toggle_cpus)
     276                list_cpus();
     277       
    204278        return 0;
    205279}
Note: See TracChangeset for help on using the changeset viewer.