Add gathering per-thread CPU usage to ddsrt

Signed-off-by: Erik Boasson <eb@ilities.com>
This commit is contained in:
Erik Boasson 2019-08-02 08:56:01 +02:00 committed by eboasson
parent 9b1920862e
commit f9808c7656
10 changed files with 589 additions and 109 deletions

View file

@ -22,12 +22,15 @@
# else
# define DDSRT_HAVE_RUSAGE 0
#endif
#else
#elif defined (_WIN32) || defined (__linux) || defined (__APPLE__)
# define DDSRT_HAVE_RUSAGE 1
#else
# define DDSRT_HAVE_RUSAGE 0
#endif
#include "dds/ddsrt/time.h"
#include "dds/ddsrt/retcode.h"
#include "dds/ddsrt/threads.h"
#if defined (__cplusplus)
extern "C" {
@ -43,8 +46,10 @@ typedef struct {
size_t nivcsw; /* Involuntary context switches. Not maintained on Windows. */
} ddsrt_rusage_t;
#define DDSRT_RUSAGE_SELF (0)
#define DDSRT_RUSAGE_THREAD (1)
enum ddsrt_getrusage_who {
DDSRT_RUSAGE_SELF,
DDSRT_RUSAGE_THREAD
};
/**
* @brief Get resource usage for the current thread or process.
@ -61,7 +66,26 @@ typedef struct {
* @retval DDS_RETCODE_ERROR
* An unidentified error occurred.
*/
DDS_EXPORT dds_return_t ddsrt_getrusage(int who, ddsrt_rusage_t *usage);
DDS_EXPORT dds_return_t ddsrt_getrusage(enum ddsrt_getrusage_who who, ddsrt_rusage_t *usage);
#if DDSRT_HAVE_THREAD_LIST
/**
* @brief Get resource usage for some thread.
*
* @param[in] tid id of the thread of to get the resource usage for
* @param[in] usage Structure where resource usage is returned.
*
* @returns A dds_return_t indicating success or failure.
*
* @retval DDS_RETCODE_OK
* Resource usage successfully returned in @usage.
* @retval DDS_RETCODE_OUT_OF_RESOURCES
* There were not enough resources to get resource usage.
* @retval DDS_RETCODE_ERROR
* An unidentified error occurred.
*/
DDS_EXPORT dds_return_t ddsrt_getrusage_anythread (ddsrt_thread_list_id_t tid, ddsrt_rusage_t * __restrict usage);
#endif
#if defined (__cplusplus)
}

View file

@ -214,6 +214,53 @@ ddsrt_thread_setname(
const char *__restrict name);
#endif
#if DDSRT_HAVE_THREAD_LIST
/**
* @brief Get a list of threads in the calling process
*
* @param[out] tids Array of size elements to be filled with thread
* identifiers, may be NULL if size is 0
* @param[in] size The size of the tids array; 0 is allowed
*
* @returns A dds_return_t indicating the number of threads in the process
* or an error code on failure.
*
* @retval > 0
* Number of threads in the process, may be larger than size
* tids[0 .. (return - 1)] are valid
* @retval DDS_RETCODE_ERROR
* Something went wrong, contents of tids is undefined
* @retval DDS_RETCODE_UNSUPPORTED
* Not supported on the platform
*/
DDS_EXPORT dds_return_t ddsrt_thread_list (ddsrt_thread_list_id_t * __restrict tids, size_t size);
/**
* @brief Get the name of the specified thread (in the calling process)
*
* @param[in] tid Thread identifier for which the name is sought
* @param[out] name Filled with the thread name (or a synthesized one)
* on successful return; name is silently truncated
* if the actual name is longer than name can hold;
* always 0-terminated if size > 0
* @param[in] size Number of bytes of name that may be assigned, size
* is 0 is allowed, though somewhat useless
*
* @returns A dds_return_t indicating success or failure.
*
* @retval DDS_RETCODE_OK
* Possibly truncated name is returned as a null-terminated
* string in name (provided size > 0).
* @retval DDS_RETCODE_NOT_FOUND
* Thread not found; the contents of name is unchanged
* @retval DDS_RETCODE_ERROR
* Unspecified failure, the contents of name is undefined
* @retval DDS_RETCODE_UNSUPPORTED
* Not supported on the platform
*/
DDS_EXPORT dds_return_t ddsrt_thread_getname_anythread (ddsrt_thread_list_id_t tid, char *__restrict name, size_t size);
#endif
/**
* @brief Push cleanup handler onto the cleanup stack
*

View file

@ -16,6 +16,7 @@
#include <task.h>
#define DDSRT_HAVE_THREAD_SETNAME (0)
#define DDSRT_HAVE_THREAD_LIST (0)
#if defined(__cplusplus)
extern "C" {

View file

@ -19,6 +19,11 @@
#else
#define DDSRT_HAVE_THREAD_SETNAME (1)
#endif
#if defined (__linux) || defined (__APPLE__)
#define DDSRT_HAVE_THREAD_LIST (1)
#else
#define DDSRT_HAVE_THREAD_LIST (0)
#endif
#if defined (__cplusplus)
extern "C" {
@ -27,6 +32,7 @@ extern "C" {
#if defined(__linux)
typedef long int ddsrt_tid_t;
#define PRIdTID "ld"
typedef long int ddsrt_thread_list_id_t;
/* __linux */
#elif defined(__FreeBSD__) && (__FreeBSD_version >= 900031)
/* FreeBSD >= 9.0 */
@ -38,6 +44,8 @@ typedef int ddsrt_tid_t;
/* macOS X >= 10.6 */
typedef uint64_t ddsrt_tid_t;
#define PRIdTID PRIu64
/* ddsrt_thread_list_id_t is actually a mach_port_t */
typedef uint32_t ddsrt_thread_list_id_t;
/* __APPLE__ */
#elif defined(__VXWORKS__)
/* TODO: Verify taskIdSelf is the right function to use on VxWorks */

View file

@ -15,6 +15,7 @@
#include "dds/ddsrt/types.h"
#define DDSRT_HAVE_THREAD_SETNAME (1)
#define DDSRT_HAVE_THREAD_LIST (1)
#if defined (__cplusplus)
extern "C" {
@ -28,6 +29,8 @@ typedef struct {
typedef DWORD ddsrt_tid_t;
#define PRIdTID "u"
typedef HANDLE ddsrt_thread_list_id_t;
#if defined (__cplusplus)
}
#endif