| 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 |
|
|---|
| 45 | #include "func.h"
|
|---|
| 46 |
|
|---|
| 47 | #define TASK_COUNT 10
|
|---|
| 48 | #define THREAD_COUNT 50
|
|---|
| 49 |
|
|---|
| 50 | /** Thread states */
|
|---|
| 51 | static const char *thread_states[] = {
|
|---|
| 52 | "Invalid",
|
|---|
| 53 | "Running",
|
|---|
| 54 | "Sleeping",
|
|---|
| 55 | "Ready",
|
|---|
| 56 | "Entering",
|
|---|
| 57 | "Exiting",
|
|---|
| 58 | "Lingering"
|
|---|
| 59 | };
|
|---|
| 60 |
|
|---|
| 61 | static void list_tasks(void)
|
|---|
| 62 | {
|
|---|
| 63 | int task_count = TASK_COUNT;
|
|---|
| 64 | task_id_t *tasks = malloc(task_count * sizeof(task_id_t));
|
|---|
| 65 | int result = get_task_ids(tasks, sizeof(task_id_t) * task_count);
|
|---|
| 66 |
|
|---|
| 67 | while (result > task_count) {
|
|---|
| 68 | task_count *= 2;
|
|---|
| 69 | tasks = realloc(tasks, task_count * sizeof(task_id_t));
|
|---|
| 70 | result = get_task_ids(tasks, sizeof(task_id_t) * task_count);
|
|---|
| 71 | }
|
|---|
| 72 |
|
|---|
| 73 | printf(" ID Threads Pages uCycles kCycles Cycle fault Name\n");
|
|---|
| 74 |
|
|---|
| 75 | int i;
|
|---|
| 76 | for (i = 0; i < result; ++i) {
|
|---|
| 77 | task_info_t taskinfo;
|
|---|
| 78 | get_task_info(tasks[i], &taskinfo);
|
|---|
| 79 | uint64_t ucycles, kcycles, fault;
|
|---|
| 80 | char usuffix, ksuffix, fsuffix;
|
|---|
| 81 | order(taskinfo.ucycles, &ucycles, &usuffix);
|
|---|
| 82 | order(taskinfo.kcycles, &kcycles, &ksuffix);
|
|---|
| 83 | order((taskinfo.kcycles + taskinfo.ucycles) - taskinfo.cycles, &fault, &fsuffix);
|
|---|
| 84 | printf("%8llu %8u %8u %12llu%c %12llu%c %12llu%c %s\n", tasks[i],
|
|---|
| 85 | taskinfo.thread_count, taskinfo.pages, ucycles, usuffix,
|
|---|
| 86 | kcycles, ksuffix, fault, fsuffix, taskinfo.name);
|
|---|
| 87 | }
|
|---|
| 88 | }
|
|---|
| 89 |
|
|---|
| 90 | static void list_threads(task_id_t taskid)
|
|---|
| 91 | {
|
|---|
| 92 | int thread_count = THREAD_COUNT;
|
|---|
| 93 | thread_info_t *threads = malloc(thread_count * sizeof(thread_info_t));
|
|---|
| 94 | int result = get_task_threads(taskid, threads, sizeof(thread_info_t) * thread_count);
|
|---|
| 95 |
|
|---|
| 96 | while (result > thread_count) {
|
|---|
| 97 | thread_count *= 2;
|
|---|
| 98 | threads = realloc(threads, thread_count * sizeof(thread_info_t));
|
|---|
| 99 | result = get_task_threads(taskid, threads, sizeof(thread_info_t) * thread_count);
|
|---|
| 100 | }
|
|---|
| 101 |
|
|---|
| 102 | if (result == 0) {
|
|---|
| 103 | printf("No task with given pid!\n");
|
|---|
| 104 | exit(1);
|
|---|
| 105 | }
|
|---|
| 106 |
|
|---|
| 107 | int i;
|
|---|
| 108 | printf(" ID State CPU Prio [k]uCycles [k]kcycles Cycle fault\n");
|
|---|
| 109 | for (i = 0; i < result; ++i) {
|
|---|
| 110 | uint64_t ucycles, kcycles, fault;
|
|---|
| 111 | char usuffix, ksuffix, fsuffix;
|
|---|
| 112 | order(threads[i].ucycles, &ucycles, &usuffix);
|
|---|
| 113 | order(threads[i].kcycles, &kcycles, &ksuffix);
|
|---|
| 114 | order((threads[i].kcycles + threads[i].ucycles) - threads[i].cycles, &fault, &fsuffix);
|
|---|
| 115 | printf("%6llu %-8s %4u %6d %12llu%c %12llu%c %12llu%c\n", threads[i].tid,
|
|---|
| 116 | thread_states[threads[i].state], threads[i].cpu,
|
|---|
| 117 | threads[i].priority, ucycles, usuffix,
|
|---|
| 118 | kcycles, ksuffix, fault, fsuffix);
|
|---|
| 119 | }
|
|---|
| 120 | }
|
|---|
| 121 |
|
|---|
| 122 | static void usage()
|
|---|
| 123 | {
|
|---|
| 124 | printf("Usage: ps [-t pid]\n");
|
|---|
| 125 | }
|
|---|
| 126 |
|
|---|
| 127 | int main(int argc, char *argv[])
|
|---|
| 128 | {
|
|---|
| 129 | --argc; ++argv;
|
|---|
| 130 |
|
|---|
| 131 | if (argc > 0)
|
|---|
| 132 | {
|
|---|
| 133 | if (str_cmp(*argv, "-t") == 0) {
|
|---|
| 134 | --argc; ++argv;
|
|---|
| 135 | if (argc != 1) {
|
|---|
| 136 | printf("Bad argument count!\n");
|
|---|
| 137 | usage();
|
|---|
| 138 | exit(1);
|
|---|
| 139 | }
|
|---|
| 140 | task_id_t taskid = strtol(*argv, NULL, 10);
|
|---|
| 141 | list_threads(taskid);
|
|---|
| 142 | } else {
|
|---|
| 143 | printf("Unknown argument %s!\n", *argv);
|
|---|
| 144 | usage();
|
|---|
| 145 | exit(1);
|
|---|
| 146 | }
|
|---|
| 147 |
|
|---|
| 148 | } else {
|
|---|
| 149 | list_tasks();
|
|---|
| 150 | }
|
|---|
| 151 |
|
|---|
| 152 | return 0;
|
|---|
| 153 | }
|
|---|
| 154 |
|
|---|
| 155 | /** @}
|
|---|
| 156 | */
|
|---|