| 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 top
|
|---|
| 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 <stdlib.h>
|
|---|
| 41 | #include <malloc.h>
|
|---|
| 42 | #include <ps.h>
|
|---|
| 43 | #include <sysinfo.h>
|
|---|
| 44 | #include "ps.h"
|
|---|
| 45 |
|
|---|
| 46 | #define TASK_COUNT 10
|
|---|
| 47 | #define THREAD_COUNT 50
|
|---|
| 48 |
|
|---|
| 49 | /** Thread states */
|
|---|
| 50 | const char *thread_states[] = {
|
|---|
| 51 | "Invalid",
|
|---|
| 52 | "Running",
|
|---|
| 53 | "Sleeping",
|
|---|
| 54 | "Ready",
|
|---|
| 55 | "Entering",
|
|---|
| 56 | "Exiting",
|
|---|
| 57 | "Lingering"
|
|---|
| 58 | };
|
|---|
| 59 |
|
|---|
| 60 | unsigned int get_tasks(task_info_t **out_infos)
|
|---|
| 61 | {
|
|---|
| 62 | int task_count = TASK_COUNT;
|
|---|
| 63 | task_id_t *tasks = malloc(task_count * sizeof(task_id_t));
|
|---|
| 64 | int result = get_task_ids(tasks, sizeof(task_id_t) * task_count);
|
|---|
| 65 |
|
|---|
| 66 | while (result > task_count) {
|
|---|
| 67 | task_count *= 2;
|
|---|
| 68 | tasks = realloc(tasks, task_count * sizeof(task_id_t));
|
|---|
| 69 | result = get_task_ids(tasks, sizeof(task_id_t) * task_count);
|
|---|
| 70 | }
|
|---|
| 71 |
|
|---|
| 72 | int i;
|
|---|
| 73 | task_info_t *taskinfos = malloc(result * sizeof(task_info_t));
|
|---|
| 74 | for (i = 0; i < result; ++i) {
|
|---|
| 75 | get_task_info(tasks[i], &taskinfos[i]);
|
|---|
| 76 | }
|
|---|
| 77 |
|
|---|
| 78 | free(tasks);
|
|---|
| 79 |
|
|---|
| 80 | *out_infos = taskinfos;
|
|---|
| 81 | return result;
|
|---|
| 82 | }
|
|---|
| 83 |
|
|---|
| 84 | thread_info_t *get_threads(task_id_t taskid)
|
|---|
| 85 | {
|
|---|
| 86 | int thread_count = THREAD_COUNT;
|
|---|
| 87 | thread_info_t *threads = malloc(thread_count * sizeof(thread_info_t));
|
|---|
| 88 | int result = get_task_threads(taskid, threads, sizeof(thread_info_t) * thread_count);
|
|---|
| 89 |
|
|---|
| 90 | while (result > thread_count) {
|
|---|
| 91 | thread_count *= 2;
|
|---|
| 92 | threads = realloc(threads, thread_count * sizeof(thread_info_t));
|
|---|
| 93 | result = get_task_threads(taskid, threads, sizeof(thread_info_t) * thread_count);
|
|---|
| 94 | }
|
|---|
| 95 |
|
|---|
| 96 | return threads;
|
|---|
| 97 | }
|
|---|
| 98 |
|
|---|
| 99 | unsigned int get_cpu_infos(uspace_cpu_info_t **out_infos)
|
|---|
| 100 | {
|
|---|
| 101 | unsigned int cpu_count = sysinfo_value("cpu.count");
|
|---|
| 102 | uspace_cpu_info_t *cpus = malloc(cpu_count * sizeof(uspace_cpu_info_t));
|
|---|
| 103 | get_cpu_info(cpus);
|
|---|
| 104 |
|
|---|
| 105 | *out_infos = cpus;
|
|---|
| 106 | return cpu_count;
|
|---|
| 107 | }
|
|---|
| 108 |
|
|---|
| 109 | /** @}
|
|---|
| 110 | */
|
|---|