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