Skip to content

Commit 00524a5

Browse files
authored
Merge pull request #22 from choco-technologies/copilot/add-thread-info-api
Add thread info API (stack usage, state, CPU usage, runtime)
2 parents e6d025e + 08d0b01 commit 00524a5

2 files changed

Lines changed: 54 additions & 0 deletions

File tree

include/dmosi.h

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -345,6 +345,33 @@ DMOD_BUILTIN_API( dmosi, 1.0, dmosi_process_t, _process_get_parent, (dmosi_proce
345345
*/
346346
typedef void (*dmosi_thread_entry_t)(void* arg);
347347

348+
/**
349+
* @brief Thread state enumeration
350+
*/
351+
typedef enum {
352+
DMOSI_THREAD_STATE_CREATED, /**< Thread created but not started */
353+
DMOSI_THREAD_STATE_READY, /**< Thread is ready to run */
354+
DMOSI_THREAD_STATE_RUNNING, /**< Thread is currently running */
355+
DMOSI_THREAD_STATE_BLOCKED, /**< Thread is blocked (waiting for resource) */
356+
DMOSI_THREAD_STATE_SUSPENDED, /**< Thread is suspended */
357+
DMOSI_THREAD_STATE_TERMINATED /**< Thread has terminated */
358+
} dmosi_thread_state_t;
359+
360+
/**
361+
* @brief Thread information structure
362+
*
363+
* This structure holds auxiliary information about a thread, including
364+
* stack usage statistics, state, CPU usage, and runtime.
365+
*/
366+
typedef struct {
367+
size_t stack_total; /**< Total stack size in bytes */
368+
size_t stack_current; /**< Current stack usage in bytes */
369+
size_t stack_peak; /**< Peak stack usage in bytes */
370+
dmosi_thread_state_t state; /**< Current thread state */
371+
float cpu_usage; /**< CPU usage as a percentage (0.0 - 100.0) */
372+
uint64_t runtime_ms; /**< Thread runtime in milliseconds */
373+
} dmosi_thread_info_t;
374+
348375
/**
349376
* @brief Create a thread
350377
*
@@ -467,6 +494,18 @@ DMOD_BUILTIN_API( dmosi, 1.0, size_t, _thread_get_all, (dmosi_thread_t* threads,
467494
*/
468495
DMOD_BUILTIN_API( dmosi, 1.0, size_t, _thread_get_by_process, (dmosi_process_t process, dmosi_thread_t* threads, size_t max_count) );
469496

497+
/**
498+
* @brief Get information about a thread
499+
*
500+
* Fills the provided @p info structure with auxiliary information about
501+
* @p thread, including stack usage statistics, state, CPU usage, and runtime.
502+
*
503+
* @param thread Thread handle (if NULL, returns info for the current thread)
504+
* @param info Pointer to a dmosi_thread_info_t structure to fill
505+
* @return int 0 on success, negative error code on failure
506+
*/
507+
DMOD_BUILTIN_API( dmosi, 1.0, int, _thread_get_info, (dmosi_thread_t thread, dmosi_thread_info_t* info) );
508+
470509
/** @} */ // end of DMOSI_THREAD_API
471510

472511
//==============================================================================

src/dmosi.c

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,21 @@ DMOD_INPUT_WEAK_API_DECLARATION( dmosi, 1.0, size_t, _thread_get_by_process, (dm
149149
return 0;
150150
}
151151

152+
DMOD_INPUT_WEAK_API_DECLARATION( dmosi, 1.0, int, _thread_get_info, (dmosi_thread_t thread, dmosi_thread_info_t* info) )
153+
{
154+
(void)thread;
155+
if (info == NULL) {
156+
return -EINVAL;
157+
}
158+
info->stack_total = 0;
159+
info->stack_current = 0;
160+
info->stack_peak = 0;
161+
info->state = DMOSI_THREAD_STATE_TERMINATED;
162+
info->cpu_usage = 0.0f;
163+
info->runtime_ms = 0;
164+
return -ENOSYS;
165+
}
166+
152167
//==============================================================================
153168
// Process API
154169
//==============================================================================

0 commit comments

Comments
 (0)