source: mainline/kernel/generic/src/proc/task.c@ 40043e8

Last change on this file since 40043e8 was 2cc569a3, checked in by Matthieu Riolo <matthieu.riolo@…>, 6 years ago

Removing inclusion of halt.h

Several files used to include halt.h even though
this was not necessary. The inclusion has therefore
been removed

  • Property mode set to 100644
File size: 16.4 KB
RevLine 
[f761f1eb]1/*
[278b4a30]2 * Copyright (c) 2010 Jakub Jermar
[ef1eab7]3 * Copyright (c) 2018 Jiri Svoboda
[f761f1eb]4 * All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 *
10 * - Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * - Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * - The name of the author may not be used to endorse or promote products
16 * derived from this software without specific prior written permission.
17 *
18 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
19 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
20 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
21 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
22 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
23 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
27 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 */
29
[174156fd]30/** @addtogroup kernel_generic_proc
[b45c443]31 * @{
32 */
33
[9179d0a]34/**
[b45c443]35 * @file
[5ba201d]36 * @brief Task management.
[9179d0a]37 */
38
[63e27ef]39#include <assert.h>
[f761f1eb]40#include <proc/thread.h>
41#include <proc/task.h>
[20d50a1]42#include <mm/as.h>
[085d973]43#include <mm/slab.h>
[31d8e10]44#include <atomic.h>
[f761f1eb]45#include <synch/spinlock.h>
[5573942]46#include <synch/waitq.h>
[f761f1eb]47#include <arch.h>
[05882233]48#include <barrier.h>
[5c9a08b]49#include <adt/list.h>
[ef1eab7]50#include <adt/odict.h>
[3f74275]51#include <cap/cap.h>
[6d9c49a]52#include <ipc/ipc.h>
[c98e6ee]53#include <ipc/ipcrsc.h>
[5d0500c]54#include <ipc/event.h>
[bab75df6]55#include <stdio.h>
[7509ddc]56#include <errno.h>
[19f857a]57#include <str.h>
[e3c762cd]58#include <syscall/copy.h>
[95ad426]59#include <macros.h>
[8e5e78f]60
[ef1eab7]61/** Spinlock protecting the @c tasks ordered dictionary. */
[da1bafb]62IRQ_SPINLOCK_INITIALIZE(tasks_lock);
[88169d9]63
[88cc71c0]64/** Ordered dictionary of active tasks by task ID.
65 *
66 * Members are task_t structures.
[88169d9]67 *
[ef1eab7]68 * The task is guaranteed to exist after it was found in the @c tasks
69 * dictionary as long as:
[5ba201d]70 *
[88169d9]71 * @li the tasks_lock is held,
[6f4495f5]72 * @li the task's lock is held when task's lock is acquired before releasing
73 * tasks_lock or
[7bb6b06]74 * @li the task's refcount is greater than 0
[88169d9]75 *
76 */
[ef1eab7]77odict_t tasks;
[88169d9]78
[286e03d]79static task_id_t task_counter = 0;
[70527f1]80
[82d515e9]81static slab_cache_t *task_cache;
[103de761]82
[121966e]83/* Forward declarations. */
84static void task_kill_internal(task_t *);
[b7fd2a0]85static errno_t tsk_constructor(void *, unsigned int);
[ef1eab7]86static size_t tsk_destructor(void *);
87
88static void *tasks_getkey(odlink_t *);
89static int tasks_cmp(void *, void *);
[121966e]90
[da1bafb]91/** Initialize kernel tasks support.
92 *
93 */
[f761f1eb]94void task_init(void)
95{
[43114c5]96 TASK = NULL;
[ef1eab7]97 odict_initialize(&tasks, tasks_getkey, tasks_cmp);
[82d515e9]98 task_cache = slab_cache_create("task_t", sizeof(task_t), 0,
[49115ac]99 tsk_constructor, tsk_destructor, 0);
[b76a2217]100}
101
[da1bafb]102/** Kill all tasks except the current task.
103 *
104 */
[f74bbaf]105void task_done(void)
106{
[da1bafb]107 size_t tasks_left;
[ef1eab7]108 task_t *task;
[2f2beb4]109
[cdc4334]110 if (ipc_box_0) {
111 task_t *task_0 = ipc_box_0->task;
112 ipc_box_0 = NULL;
[2f2beb4]113 /*
114 * The first task is held by kinit(), we need to release it or
115 * it will never finish cleanup.
116 */
117 task_release(task_0);
118 }
[a35b458]119
[da1bafb]120 /* Repeat until there are any tasks except TASK */
121 do {
[121966e]122#ifdef CONFIG_DEBUG
123 printf("Killing tasks... ");
124#endif
[da1bafb]125 irq_spinlock_lock(&tasks_lock, true);
[121966e]126 tasks_left = 0;
[ef1eab7]127
[aab5e46]128 task = task_first();
129 while (task != NULL) {
[ef1eab7]130 if (task != TASK) {
131 tasks_left++;
132#ifdef CONFIG_DEBUG
133 printf("[%" PRIu64 "] ", task->taskid);
134#endif
135 task_kill_internal(task);
136 }
137
[aab5e46]138 task = task_next(task);
[ef1eab7]139 }
140
[da1bafb]141 irq_spinlock_unlock(&tasks_lock, true);
[a35b458]142
[121966e]143 thread_sleep(1);
[a35b458]144
[f74bbaf]145#ifdef CONFIG_DEBUG
[121966e]146 printf("\n");
147#endif
[da1bafb]148 } while (tasks_left > 0);
[f74bbaf]149}
[70527f1]150
[b7fd2a0]151errno_t tsk_constructor(void *obj, unsigned int kmflags)
[59ee56f]152{
[da1bafb]153 task_t *task = (task_t *) obj;
[c46bfbc]154
[b7fd2a0]155 errno_t rc = caps_task_alloc(task);
[c46bfbc]156 if (rc != EOK)
157 return rc;
[a35b458]158
[e3306d04]159 atomic_store(&task->refcount, 0);
160 atomic_store(&task->lifecount, 0);
[a35b458]161
[da1bafb]162 irq_spinlock_initialize(&task->lock, "task_t_lock");
[a35b458]163
[55b77d9]164 list_initialize(&task->threads);
[a35b458]165
[da1bafb]166 ipc_answerbox_init(&task->answerbox, task);
[a35b458]167
[86939b1]168 spinlock_initialize(&task->active_calls_lock, "active_calls_lock");
169 list_initialize(&task->active_calls);
[a35b458]170
[59ee56f]171#ifdef CONFIG_UDEBUG
172 /* Init kbox stuff */
[da1bafb]173 task->kb.thread = NULL;
174 ipc_answerbox_init(&task->kb.box, task);
175 mutex_initialize(&task->kb.cleanup_lock, MUTEX_PASSIVE);
[59ee56f]176#endif
[a35b458]177
[7f11dc6]178 return EOK;
[59ee56f]179}
180
[49115ac]181size_t tsk_destructor(void *obj)
182{
183 task_t *task = (task_t *) obj;
[a35b458]184
[3f74275]185 caps_task_free(task);
[49115ac]186 return 0;
187}
188
[814c4f5]189/** Create new task with no threads.
[70527f1]190 *
[5ba201d]191 * @param as Task's address space.
192 * @param name Symbolic name (a copy is made).
[70527f1]193 *
[5ba201d]194 * @return New task's structure.
[70527f1]195 *
196 */
[a000878c]197task_t *task_create(as_t *as, const char *name)
[f761f1eb]198{
[abf6c01]199 task_t *task = (task_t *) slab_alloc(task_cache, FRAME_ATOMIC);
200 if (!task)
[6a32cc5f]201 return NULL;
[a35b458]202
[da1bafb]203 task_create_arch(task);
[a35b458]204
[da1bafb]205 task->as = as;
206 str_cpy(task->name, TASK_NAME_BUFLEN, name);
[a35b458]207
[473d5d2]208 task->container = CONTAINER;
[719a208]209 task->perms = 0;
[da1bafb]210 task->ucycles = 0;
211 task->kcycles = 0;
[7ad17de]212
[3f74275]213 caps_task_init(task);
[e9d15d9]214
[da1bafb]215 task->ipc_info.call_sent = 0;
[be06914]216 task->ipc_info.call_received = 0;
[da1bafb]217 task->ipc_info.answer_sent = 0;
[be06914]218 task->ipc_info.answer_received = 0;
219 task->ipc_info.irq_notif_received = 0;
[da1bafb]220 task->ipc_info.forwarded = 0;
[5d0500c]221
222 event_task_init(task);
[a35b458]223
[c33f39f]224 task->answerbox.active = true;
225
[9a1b20c]226#ifdef CONFIG_UDEBUG
227 /* Init debugging stuff */
[da1bafb]228 udebug_task_init(&task->udebug);
[a35b458]229
[9a1b20c]230 /* Init kbox stuff */
[c33f39f]231 task->kb.box.active = true;
[da1bafb]232 task->kb.finished = false;
[9a1b20c]233#endif
[a35b458]234
[cdc4334]235 if ((ipc_box_0) &&
236 (container_check(ipc_box_0->task->container, task->container))) {
[eadaeae8]237 cap_phone_handle_t phone_handle;
[334c103]238 errno_t rc = phone_alloc(task, true, &phone_handle, NULL);
[09d01f2]239 if (rc != EOK) {
[6a32cc5f]240 task->as = NULL;
241 task_destroy_arch(task);
242 slab_free(task_cache, task);
243 return NULL;
244 }
[a35b458]245
[48bcf49]246 kobject_t *phone_obj = kobject_get(task, phone_handle,
247 KOBJECT_TYPE_PHONE);
[cdc4334]248 (void) ipc_phone_connect(phone_obj->phone, ipc_box_0);
[05ffb41]249 }
[a35b458]250
[da1bafb]251 irq_spinlock_lock(&tasks_lock, true);
[a35b458]252
[da1bafb]253 task->taskid = ++task_counter;
[ef1eab7]254 odlink_initialize(&task->ltasks);
255 odict_insert(&task->ltasks, &tasks, NULL);
[a35b458]256
[da1bafb]257 irq_spinlock_unlock(&tasks_lock, true);
[a35b458]258
[da1bafb]259 return task;
[f761f1eb]260}
261
[7509ddc]262/** Destroy task.
263 *
[da1bafb]264 * @param task Task to be destroyed.
[5ba201d]265 *
[7509ddc]266 */
[da1bafb]267void task_destroy(task_t *task)
[7509ddc]268{
[ea7890e7]269 /*
[ca4c5596]270 * Remove the task from the task odict.
[ea7890e7]271 */
[da1bafb]272 irq_spinlock_lock(&tasks_lock, true);
[ef1eab7]273 odict_remove(&task->ltasks);
[da1bafb]274 irq_spinlock_unlock(&tasks_lock, true);
[a35b458]275
[ea7890e7]276 /*
277 * Perform architecture specific task destruction.
278 */
[da1bafb]279 task_destroy_arch(task);
[a35b458]280
[ea7890e7]281 /*
282 * Drop our reference to the address space.
283 */
[da1bafb]284 as_release(task->as);
[a35b458]285
[82d515e9]286 slab_free(task_cache, task);
[7509ddc]287}
288
[278b4a30]289/** Hold a reference to a task.
290 *
291 * Holding a reference to a task prevents destruction of that task.
292 *
[da1bafb]293 * @param task Task to be held.
294 *
[278b4a30]295 */
[da1bafb]296void task_hold(task_t *task)
[278b4a30]297{
[da1bafb]298 atomic_inc(&task->refcount);
[278b4a30]299}
300
301/** Release a reference to a task.
302 *
303 * The last one to release a reference to a task destroys the task.
304 *
[da1bafb]305 * @param task Task to be released.
306 *
[278b4a30]307 */
[da1bafb]308void task_release(task_t *task)
[278b4a30]309{
[da1bafb]310 if ((atomic_predec(&task->refcount)) == 0)
311 task_destroy(task);
[278b4a30]312}
313
[dd8d5a7]314#ifdef __32_BITS__
315
316/** Syscall for reading task ID from userspace (32 bits)
[ec55358]317 *
[dd8d5a7]318 * @param uspace_taskid Pointer to user-space buffer
319 * where to store current task ID.
[5ba201d]320 *
321 * @return Zero on success or an error code from @ref errno.h.
[ec55358]322 *
323 */
[5a5269d]324sys_errno_t sys_task_get_id(uspace_ptr_sysarg64_t uspace_taskid)
[ec55358]325{
326 /*
[814c4f5]327 * No need to acquire lock on TASK because taskid remains constant for
328 * the lifespan of the task.
[ec55358]329 */
[b7fd2a0]330 return (sys_errno_t) copy_to_uspace(uspace_taskid, &TASK->taskid,
[6f4495f5]331 sizeof(TASK->taskid));
[ec55358]332}
333
[dd8d5a7]334#endif /* __32_BITS__ */
335
336#ifdef __64_BITS__
337
338/** Syscall for reading task ID from userspace (64 bits)
339 *
340 * @return Current task ID.
341 *
342 */
343sysarg_t sys_task_get_id(void)
344{
345 /*
346 * No need to acquire lock on TASK because taskid remains constant for
347 * the lifespan of the task.
348 */
349 return TASK->taskid;
350}
351
352#endif /* __64_BITS__ */
353
[bc18d63]354/** Syscall for setting the task name.
355 *
356 * The name simplifies identifying the task in the task list.
357 *
[5ba201d]358 * @param name The new name for the task. (typically the same
359 * as the command used to execute it).
[bc18d63]360 *
361 * @return 0 on success or an error code from @ref errno.h.
[5ba201d]362 *
[bc18d63]363 */
[5a5269d]364sys_errno_t sys_task_set_name(const uspace_ptr_char uspace_name, size_t name_len)
[bc18d63]365{
366 char namebuf[TASK_NAME_BUFLEN];
[a35b458]367
[bc18d63]368 /* Cap length of name and copy it from userspace. */
369 if (name_len > TASK_NAME_BUFLEN - 1)
370 name_len = TASK_NAME_BUFLEN - 1;
[a35b458]371
[b7fd2a0]372 errno_t rc = copy_from_uspace(namebuf, uspace_name, name_len);
[a53ed3a]373 if (rc != EOK)
[b7fd2a0]374 return (sys_errno_t) rc;
[a35b458]375
[f4b1535]376 namebuf[name_len] = '\0';
[a35b458]377
[577f042a]378 /*
379 * As the task name is referenced also from the
380 * threads, lock the threads' lock for the course
381 * of the update.
382 */
[a35b458]383
[577f042a]384 irq_spinlock_lock(&tasks_lock, true);
385 irq_spinlock_lock(&TASK->lock, false);
386 irq_spinlock_lock(&threads_lock, false);
[a35b458]387
[577f042a]388 /* Set task name */
[f4b1535]389 str_cpy(TASK->name, TASK_NAME_BUFLEN, namebuf);
[a35b458]390
[577f042a]391 irq_spinlock_unlock(&threads_lock, false);
392 irq_spinlock_unlock(&TASK->lock, false);
393 irq_spinlock_unlock(&tasks_lock, true);
[a35b458]394
[bc18d63]395 return EOK;
396}
397
[1e9f8ab]398/** Syscall to forcefully terminate a task
399 *
400 * @param uspace_taskid Pointer to task ID in user space.
401 *
402 * @return 0 on success or an error code from @ref errno.h.
403 *
404 */
[5a5269d]405sys_errno_t sys_task_kill(uspace_ptr_task_id_t uspace_taskid)
[1e9f8ab]406{
407 task_id_t taskid;
[b7fd2a0]408 errno_t rc = copy_from_uspace(&taskid, uspace_taskid, sizeof(taskid));
[a53ed3a]409 if (rc != EOK)
[b7fd2a0]410 return (sys_errno_t) rc;
[a35b458]411
[b7fd2a0]412 return (sys_errno_t) task_kill(taskid);
[1e9f8ab]413}
414
[9a8d91b]415/** Find task structure corresponding to task ID.
416 *
[814c4f5]417 * The tasks_lock must be already held by the caller of this function and
418 * interrupts must be disabled.
[9a8d91b]419 *
[5ba201d]420 * @param id Task ID.
421 *
[e1b6742]422 * @return Task structure address or NULL if there is no such task ID.
[9a8d91b]423 *
424 */
[5ba201d]425task_t *task_find_by_id(task_id_t id)
426{
[63e27ef]427 assert(interrupts_disabled());
428 assert(irq_spinlock_locked(&tasks_lock));
[1d432f9]429
[ef1eab7]430 odlink_t *odlink = odict_find_eq(&tasks, &id, NULL);
431 if (odlink != NULL)
432 return odict_get_instance(odlink, task_t, ltasks);
[a35b458]433
[b76a2217]434 return NULL;
[9a8d91b]435}
436
[aab5e46]437/** Get count of tasks.
438 *
439 * @return Number of tasks in the system
440 */
441size_t task_count(void)
442{
443 assert(interrupts_disabled());
444 assert(irq_spinlock_locked(&tasks_lock));
445
446 return odict_count(&tasks);
447}
448
449/** Get first task (task with lowest ID).
450 *
451 * @return Pointer to first task or @c NULL if there are none.
452 */
453task_t *task_first(void)
454{
455 odlink_t *odlink;
456
457 assert(interrupts_disabled());
458 assert(irq_spinlock_locked(&tasks_lock));
459
460 odlink = odict_first(&tasks);
461 if (odlink == NULL)
462 return NULL;
463
464 return odict_get_instance(odlink, task_t, ltasks);
465}
466
467/** Get next task (with higher task ID).
468 *
469 * @param cur Current task
470 * @return Pointer to next task or @c NULL if there are no more tasks.
471 */
472task_t *task_next(task_t *cur)
473{
474 odlink_t *odlink;
475
476 assert(interrupts_disabled());
477 assert(irq_spinlock_locked(&tasks_lock));
478
479 odlink = odict_next(&cur->ltasks, &tasks);
480 if (odlink == NULL)
481 return NULL;
482
483 return odict_get_instance(odlink, task_t, ltasks);
484}
485
[0313ff0]486/** Get accounting data of given task.
487 *
[1d432f9]488 * Note that task lock of 'task' must be already held and interrupts must be
[814c4f5]489 * already disabled.
[0313ff0]490 *
[da1bafb]491 * @param task Pointer to the task.
[88dea9d]492 * @param ucycles Out pointer to sum of all user cycles.
493 * @param kcycles Out pointer to sum of all kernel cycles.
[0313ff0]494 *
495 */
[da1bafb]496void task_get_accounting(task_t *task, uint64_t *ucycles, uint64_t *kcycles)
[0313ff0]497{
[63e27ef]498 assert(interrupts_disabled());
499 assert(irq_spinlock_locked(&task->lock));
[1d432f9]500
[a2a00e8]501 /* Accumulated values of task */
[da1bafb]502 uint64_t uret = task->ucycles;
503 uint64_t kret = task->kcycles;
[a35b458]504
[0313ff0]505 /* Current values of threads */
[feeac0d]506 list_foreach(task->threads, th_link, thread_t, thread) {
[da1bafb]507 irq_spinlock_lock(&thread->lock, false);
[a35b458]508
[62b6d17]509 /* Process only counted threads */
[da1bafb]510 if (!thread->uncounted) {
511 if (thread == THREAD) {
[6f4495f5]512 /* Update accounting of current thread */
[a2a00e8]513 thread_update_accounting(false);
[da1bafb]514 }
[a35b458]515
[da1bafb]516 uret += thread->ucycles;
517 kret += thread->kcycles;
[62b6d17]518 }
[a35b458]519
[da1bafb]520 irq_spinlock_unlock(&thread->lock, false);
[0313ff0]521 }
[a35b458]522
[a2a00e8]523 *ucycles = uret;
524 *kcycles = kret;
[0313ff0]525}
526
[da1bafb]527static void task_kill_internal(task_t *task)
[121966e]528{
[df58e44]529 irq_spinlock_lock(&task->lock, false);
530 irq_spinlock_lock(&threads_lock, false);
[a35b458]531
[121966e]532 /*
533 * Interrupt all threads.
534 */
[a35b458]535
[feeac0d]536 list_foreach(task->threads, th_link, thread_t, thread) {
[121966e]537 bool sleeping = false;
[a35b458]538
[da1bafb]539 irq_spinlock_lock(&thread->lock, false);
[a35b458]540
[da1bafb]541 thread->interrupted = true;
542 if (thread->state == Sleeping)
[121966e]543 sleeping = true;
[a35b458]544
[da1bafb]545 irq_spinlock_unlock(&thread->lock, false);
[a35b458]546
[121966e]547 if (sleeping)
[da1bafb]548 waitq_interrupt_sleep(thread);
[121966e]549 }
[a35b458]550
[df58e44]551 irq_spinlock_unlock(&threads_lock, false);
[da1bafb]552 irq_spinlock_unlock(&task->lock, false);
[121966e]553}
554
[7509ddc]555/** Kill task.
[ea7890e7]556 *
557 * This function is idempotent.
558 * It signals all the task's threads to bail it out.
[7509ddc]559 *
[5ba201d]560 * @param id ID of the task to be killed.
561 *
562 * @return Zero on success or an error code from errno.h.
[7509ddc]563 *
564 */
[b7fd2a0]565errno_t task_kill(task_id_t id)
[7509ddc]566{
[9b6aae6]567 if (id == 1)
568 return EPERM;
[a35b458]569
[da1bafb]570 irq_spinlock_lock(&tasks_lock, true);
[a35b458]571
[da1bafb]572 task_t *task = task_find_by_id(id);
573 if (!task) {
574 irq_spinlock_unlock(&tasks_lock, true);
[7509ddc]575 return ENOENT;
576 }
[a35b458]577
[da1bafb]578 task_kill_internal(task);
579 irq_spinlock_unlock(&tasks_lock, true);
[a35b458]580
[da1bafb]581 return EOK;
[7509ddc]582}
583
[5bcf1f9]584/** Kill the currently running task.
585 *
586 * @param notify Send out fault notifications.
587 *
588 * @return Zero on success or an error code from errno.h.
589 *
590 */
591void task_kill_self(bool notify)
592{
593 /*
594 * User space can subscribe for FAULT events to take action
595 * whenever a task faults (to take a dump, run a debugger, etc.).
596 * The notification is always available, but unless udebug is enabled,
597 * that's all you get.
[ae7d03c]598 */
[5bcf1f9]599 if (notify) {
[f9061b4]600 /* Notify the subscriber that a fault occurred. */
601 if (event_notify_3(EVENT_FAULT, false, LOWER32(TASK->taskid),
602 UPPER32(TASK->taskid), (sysarg_t) THREAD) == EOK) {
[5bcf1f9]603#ifdef CONFIG_UDEBUG
604 /* Wait for a debugging session. */
605 udebug_thread_fault();
606#endif
607 }
608 }
[a35b458]609
[5bcf1f9]610 irq_spinlock_lock(&tasks_lock, true);
611 task_kill_internal(TASK);
612 irq_spinlock_unlock(&tasks_lock, true);
[a35b458]613
[5bcf1f9]614 thread_exit();
615}
616
617/** Process syscall to terminate the current task.
618 *
619 * @param notify Send out fault notifications.
620 *
621 */
[b7fd2a0]622sys_errno_t sys_task_exit(sysarg_t notify)
[5bcf1f9]623{
624 task_kill_self(notify);
[88e43bc]625 unreachable();
[5bcf1f9]626}
627
[ef1eab7]628static void task_print(task_t *task, bool additional)
[b76a2217]629{
[da1bafb]630 irq_spinlock_lock(&task->lock, false);
[a35b458]631
[a2a00e8]632 uint64_t ucycles;
633 uint64_t kcycles;
[1ba37fa]634 char usuffix, ksuffix;
[da1bafb]635 task_get_accounting(task, &ucycles, &kcycles);
[e535eeb]636 order_suffix(ucycles, &ucycles, &usuffix);
637 order_suffix(kcycles, &kcycles, &ksuffix);
[a35b458]638
[da1bafb]639#ifdef __32_BITS__
[ef1eab7]640 if (additional)
[077842c]641 printf("%-8" PRIu64 " %9zu", task->taskid,
[036e97c]642 atomic_load(&task->refcount));
[c0f13d2]643 else
644 printf("%-8" PRIu64 " %-14s %-5" PRIu32 " %10p %10p"
645 " %9" PRIu64 "%c %9" PRIu64 "%c\n", task->taskid,
[473d5d2]646 task->name, task->container, task, task->as,
[c0f13d2]647 ucycles, usuffix, kcycles, ksuffix);
[52755f1]648#endif
[a35b458]649
[52755f1]650#ifdef __64_BITS__
[ef1eab7]651 if (additional)
[7e752b2]652 printf("%-8" PRIu64 " %9" PRIu64 "%c %9" PRIu64 "%c "
[077842c]653 "%9zu\n", task->taskid, ucycles, usuffix, kcycles,
[036e97c]654 ksuffix, atomic_load(&task->refcount));
[c0f13d2]655 else
656 printf("%-8" PRIu64 " %-14s %-5" PRIu32 " %18p %18p\n",
[473d5d2]657 task->taskid, task->name, task->container, task, task->as);
[52755f1]658#endif
[a35b458]659
[da1bafb]660 irq_spinlock_unlock(&task->lock, false);
[b76a2217]661}
662
[c0f13d2]663/** Print task list
664 *
665 * @param additional Print additional information.
666 *
667 */
668void task_print_list(bool additional)
[37c57f2]669{
[f74bbaf]670 /* Messing with task structures, avoid deadlock */
[da1bafb]671 irq_spinlock_lock(&tasks_lock, true);
[a35b458]672
[5ba201d]673#ifdef __32_BITS__
[c0f13d2]674 if (additional)
[48dcc69]675 printf("[id ] [threads] [calls] [callee\n");
[c0f13d2]676 else
[473d5d2]677 printf("[id ] [name ] [ctn] [address ] [as ]"
[c0f13d2]678 " [ucycles ] [kcycles ]\n");
[52755f1]679#endif
[a35b458]680
[52755f1]681#ifdef __64_BITS__
[c0f13d2]682 if (additional)
[be06914]683 printf("[id ] [ucycles ] [kcycles ] [threads] [calls]"
[c0f13d2]684 " [callee\n");
685 else
[473d5d2]686 printf("[id ] [name ] [ctn] [address ]"
[c0f13d2]687 " [as ]\n");
[52755f1]688#endif
[a35b458]689
[ef1eab7]690 task_t *task;
691
[aab5e46]692 task = task_first();
693 while (task != NULL) {
[ef1eab7]694 task_print(task, additional);
[aab5e46]695 task = task_next(task);
[ef1eab7]696 }
[a35b458]697
[da1bafb]698 irq_spinlock_unlock(&tasks_lock, true);
[37c57f2]699}
[7509ddc]700
[ef1eab7]701/** Get key function for the @c tasks ordered dictionary.
702 *
703 * @param odlink Link
704 * @return Pointer to task ID cast as 'void *'
705 */
706static void *tasks_getkey(odlink_t *odlink)
707{
708 task_t *task = odict_get_instance(odlink, task_t, ltasks);
709 return (void *) &task->taskid;
710}
711
712/** Key comparison function for the @c tasks ordered dictionary.
713 *
714 * @param a Pointer to thread A ID
715 * @param b Pointer to thread B ID
716 * @return -1, 0, 1 iff ID A is less than, equal to, greater than B
717 */
718static int tasks_cmp(void *a, void *b)
719{
720 task_id_t ida = *(task_id_t *)a;
721 task_id_t idb = *(task_id_t *)b;
722
723 if (ida < idb)
724 return -1;
725 else if (ida == idb)
726 return 0;
727 else
728 return +1;
729}
730
[cc73a8a1]731/** @}
[b45c443]732 */
Note: See TracBrowser for help on using the repository browser.