Changeset e1b6742 in mainline for uspace


Ignore:
Timestamp:
2010-04-18T12:17:11Z (15 years ago)
Author:
Martin Decky <martin@…>
Branches:
lfn, master, serial, ticket/834-toolchain-update, topic/msim-upgrade, topic/simplify-dev-export
Children:
e535eeb
Parents:
bbda5ab
Message:

export threads to user space
the "tasks" command can now print all threads or threads belonging to a task

Location:
uspace
Files:
3 edited

Legend:

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

    rbbda5ab re1b6742  
    11/*
    22 * Copyright (c) 2010 Stanislav Kozina
     3 * Copyright (c) 2010 Martin Decky
    34 * All rights reserved.
    45 *
     
    4344#include <malloc.h>
    4445#include <inttypes.h>
     46#include <bool.h>
    4547#include <arg_parse.h>
    4648#include "func.h"
     
    5355#define PRINT_LOAD1(x)  ((x) >> 11)
    5456#define PRINT_LOAD2(x)  (((x) & 0x7ff) / 2)
    55 
    56 /** Thread states */
    57 //static const char *thread_states[] = {
    58 //      "Invalid",
    59 //      "Running",
    60 //      "Sleeping",
    61 //      "Ready",
    62 //      "Entering",
    63 //      "Exiting",
    64 //      "Lingering"
    65 //};
    6657
    6758static void list_tasks(void)
     
    9081                       
    9182                        printf("%8" PRIu64 "%8u %8" PRIu64"%c %12"
    92                             PRIu64"%c %12" PRIu64"%c %s\n", ids[i], stats_task->threads,
     83                            PRIu64 "%c %12" PRIu64 "%c %s\n", ids[i], stats_task->threads,
    9384                            virtmem, vmsuffix, ucycles, usuffix, kcycles, ksuffix,
    9485                            stats_task->name);
     
    10293}
    10394
    104 static void list_threads(task_id_t task_id)
    105 {
    106         /* TODO:
    107         size_t thread_count = THREAD_COUNT;
    108         thread_info_t *threads = malloc(thread_count * sizeof(thread_info_t));
    109         size_t result = get_task_threads(threads, sizeof(thread_info_t) * thread_count);
    110 
    111         while (result > thread_count) {
    112                 thread_count *= 2;
    113                 threads = realloc(threads, thread_count * sizeof(thread_info_t));
    114                 result = get_task_threads(threads, sizeof(thread_info_t) * thread_count);
    115         }
    116 
    117         if (result == 0) {
    118                 printf("No task with given pid!\n");
    119                 exit(1);
    120         }
    121 
    122         size_t i;
     95static void list_threads(task_id_t task_id, bool all)
     96{
     97        size_t count;
     98        thread_id_t *ids =
     99            (thread_id_t *) stats_get_threads(&count);
     100       
     101        if (ids == NULL) {
     102                fprintf(stderr, "%s: Unable to get threads\n", NAME);
     103                return;
     104        }
     105       
    123106        printf("    ID    State  CPU   Prio    [k]uCycles    [k]kcycles   Cycle fault\n");
    124         for (i = 0; i < result; ++i) {
    125                 if (threads[i].taskid != taskid) {
    126                         continue;
    127                 }
    128                 uint64_t ucycles, kcycles;
    129                 char usuffix, ksuffix;
    130                 order(threads[i].ucycles, &ucycles, &usuffix);
    131                 order(threads[i].kcycles, &kcycles, &ksuffix);
    132                 printf("%6llu %-8s %4u %6d %12llu%c %12llu%c\n", threads[i].tid,
    133                         thread_states[threads[i].state], threads[i].cpu,
    134                         threads[i].priority, ucycles, usuffix,
    135                         kcycles, ksuffix);
    136         }
    137 
    138         free(threads); */
     107        size_t i;
     108        for (i = 0; i < count; i++) {
     109                stats_thread_t *stats_thread = stats_get_thread(ids[i]);
     110                if (stats_thread != NULL) {
     111                        if ((all) || (stats_thread->task_id == task_id)) {
     112                                uint64_t ucycles, kcycles;
     113                                char usuffix, ksuffix;
     114                               
     115                                order(stats_thread->ucycles, &ucycles, &usuffix);
     116                                order(stats_thread->kcycles, &kcycles, &ksuffix);
     117                               
     118                                if (stats_thread->on_cpu) {
     119                                        printf("%8" PRIu64 " %-8s %4u %6d %12"
     120                                            PRIu64"%c %12" PRIu64"%c\n", ids[i],
     121                                            thread_get_state(stats_thread->state),
     122                                            stats_thread->cpu, stats_thread->priority,
     123                                            ucycles, usuffix, kcycles, ksuffix);
     124                                } else {
     125                                        printf("%8" PRIu64 " %-8s ---- %6d %12"
     126                                            PRIu64"%c %12" PRIu64"%c\n", ids[i],
     127                                            thread_get_state(stats_thread->state),
     128                                            stats_thread->priority,
     129                                            ucycles, usuffix, kcycles, ksuffix);
     130                                }
     131                        }
     132                       
     133                        free(stats_thread);
     134                } else if (all)
     135                        printf("%8" PRIu64 "\n", ids[i]);
     136        }
     137       
     138        free(ids);
    139139}
    140140
     
    190190{
    191191        printf(
    192             "Usage: tasks [-t task_id] [-l] [-c]\n" \
     192            "Usage: tasks [-t task_id] [-a] [-l] [-c]\n" \
    193193            "\n" \
    194194            "Options:\n" \
     
    197197            "\t\tList threads of the given task\n" \
    198198            "\n" \
     199            "\t-a\n" \
     200            "\t--all\n" \
     201            "\t\tList all threads\n" \
     202            "\n" \
    199203            "\t-l\n" \
    200204            "\t--load\n" \
     
    217221        bool toggle_tasks = true;
    218222        bool toggle_threads = false;
     223        bool toggle_all = false;
    219224        bool toggle_load = false;
    220225        bool toggle_cpus = false;
    221226       
    222         task_id_t task_id;
     227        task_id_t task_id = 0;
    223228       
    224229        int i;
     
    230235                        usage();
    231236                        return 0;
     237                }
     238               
     239                /* All threads */
     240                if ((off = arg_parse_short_long(argv[i], "-a", "--all")) != -1) {
     241                        toggle_tasks = false;
     242                        toggle_threads = true;
     243                        toggle_all = true;
     244                        continue;
    232245                }
    233246               
     
    268281       
    269282        if (toggle_threads)
    270                 list_threads(task_id);
     283                list_threads(task_id, toggle_all);
    271284       
    272285        if (toggle_load)
  • uspace/lib/c/generic/stats.c

    rbbda5ab re1b6742  
    4343#define SYSINFO_STATS_MAX_PATH  64
    4444
     45/** Thread states
     46 *
     47 */
     48static const char *thread_states[] = {
     49        "Invalid",
     50        "Running",
     51        "Sleeping",
     52        "Ready",
     53        "Entering",
     54        "Exiting",
     55        "Lingering"
     56};
     57
    4558/** Get CPUs statistics
    4659 *
     
    125138       
    126139        return stats_task;
     140}
     141
     142/** Get thread IDs
     143 *
     144 * @param count Number of IDs returned.
     145 *
     146 * @return Array of IDs (thread_id_t).
     147 *         If non-NULL then it should be eventually freed
     148 *         by free().
     149 *
     150 */
     151thread_id_t *stats_get_threads(size_t *count)
     152{
     153        size_t size = 0;
     154        thread_id_t *ids =
     155            (thread_id_t *) sysinfo_get_data("system.threads", &size);
     156       
     157        assert((size % sizeof(thread_id_t)) == 0);
     158       
     159        *count = size / sizeof(thread_id_t);
     160        return ids;
     161}
     162
     163/** Get single thread statistics
     164 *
     165 * @param thread_id Thread ID we are interested in.
     166 *
     167 * @return Pointer to the stats_thread_t structure.
     168 *         If non-NULL then it should be eventually freed
     169 *         by free().
     170 *
     171 */
     172stats_thread_t *stats_get_thread(thread_id_t thread_id)
     173{
     174        char name[SYSINFO_STATS_MAX_PATH];
     175        snprintf(name, SYSINFO_STATS_MAX_PATH, "system.threads.%" PRIu64, thread_id);
     176       
     177        size_t size = 0;
     178        stats_thread_t *stats_thread =
     179            (stats_thread_t *) sysinfo_get_data(name, &size);
     180       
     181        assert((size == sizeof(stats_thread_t)) || (size == 0));
     182       
     183        return stats_thread;
    127184}
    128185
     
    188245}
    189246
     247const char *thread_get_state(state_t state)
     248{
     249        if (state <= Lingering)
     250                return thread_states[state];
     251       
     252        return thread_states[Invalid];
     253}
     254
    190255/** @}
    191256 */
  • uspace/lib/c/include/stats.h

    rbbda5ab re1b6742  
    3737
    3838#include <task.h>
     39#include <thread.h>
     40#include <stdint.h>
     41#include <bool.h>
    3942#include <kernel/sysinfo/abi.h>
    4043
    4144extern stats_cpu_t *stats_get_cpus(size_t *);
    4245extern stats_physmem_t *stats_get_physmem(void);
    43 extern task_id_t *stats_get_tasks(size_t *);
    44 extern stats_task_t *stats_get_task(task_id_t);
    4546extern load_t *stats_get_load(size_t *);
    4647extern sysarg_t stats_get_uptime(void);
    4748
     49extern task_id_t *stats_get_tasks(size_t *);
     50extern stats_task_t *stats_get_task(task_id_t);
     51
     52extern thread_id_t *stats_get_threads(size_t *);
     53extern stats_thread_t *stats_get_thread(thread_id_t);
     54
    4855extern void stats_print_load_fragment(load_t, unsigned int);
     56extern const char *thread_get_state(state_t);
    4957
    5058#endif
Note: See TracChangeset for help on using the changeset viewer.