Skip to content

time: handle CLOCK_THREAD_CPUTIME_ID#455

Draft
oI0ck wants to merge 2 commits intomasterfrom
michal.lach/cputime
Draft

time: handle CLOCK_THREAD_CPUTIME_ID#455
oI0ck wants to merge 2 commits intomasterfrom
michal.lach/cputime

Conversation

@oI0ck
Copy link
Copy Markdown
Member

@oI0ck oI0ck commented Dec 5, 2025

Description

This pull request adds support for POSIX specified CLOCK_THREAD_CPUTIME_ID flag in clock_gettime

Types of changes

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to change)
  • Chore (refactoring, style fixes, git/CI config, submodule management, no code logic changes)

How Has This Been Tested?

  • Already covered by automatic testing.
  • New test added: (add PR link here).
  • Tested by hand on: (list targets here).

Checklist:

  • My change requires a change to the documentation.
  • I have updated the documentation accordingly.
  • I have added tests to cover my changes.
  • All new and existing linter checks and tests passed.
  • My changes generate no new compilation warnings for any of the targets.

Special treatment

@gemini-code-assist
Copy link
Copy Markdown

Summary of Changes

Hello @oI0ck, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed!

This pull request introduces a significant enhancement to the system's timekeeping capabilities by integrating support for CLOCK_THREAD_CPUTIME_ID. This allows applications to accurately measure the CPU time consumed by individual threads, aligning with POSIX standards and providing more granular performance monitoring. The change involves updating header definitions and modifying the core clock_gettime implementation to leverage new thread information retrieval mechanisms.

Highlights

  • New Clock ID Support: Added support for the POSIX specified CLOCK_THREAD_CPUTIME_ID flag within the clock_gettime function, allowing retrieval of a thread's CPU time.
  • threadsinfo Function Update: The signature of the threadsinfo function in include/sys/threads.h has been modified to accept additional parameters (tid, flags, n) to support more detailed thread information queries.
  • clock_gettime Implementation: The clock_gettime function in time/time.c now includes logic to handle CLOCK_THREAD_CPUTIME_ID by calling gettid() and the updated threadsinfo() function to fetch the current thread's CPU time.
Using Gemini Code Assist

The full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips.

Invoking Gemini

You can request assistance from Gemini at any point by creating a comment using either /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands on the current page.

Feature Command Description
Code Review /gemini review Performs a code review for the current pull request in its current state.
Pull Request Summary /gemini summary Provides a summary of the current pull request in its current state.
Comment @gemini-code-assist Responds in comments when explicitly tagged, both in pull request comments and review comments.
Help /gemini help Displays a list of available commands.

Customization

To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a .gemini/ folder in the base of the repository. Detailed instructions can be found here.

Limitations & Feedback

Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for Github and other Google products, sign up here.

You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension.

Footnotes

  1. Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution.

Copy link
Copy Markdown

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request adds support for CLOCK_THREAD_CPUTIME_ID to clock_gettime. The implementation is a good start, but there are a few important issues to address. The signature of threadsinfo() has been modified, which constitutes a breaking API change that contradicts the PR description. Additionally, in the new logic for clock_gettime, there's an unnecessary function call and missing error handling, which could lead to incorrect behavior. I've provided suggestions to fix these issues and improve the code's robustness and maintainability.



extern int threadsinfo(int n, threadinfo_t *info);
extern int threadsinfo(int *tid, unsigned int flags, int n, threadinfo_t *info);
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

high

The signature of the extern function threadsinfo has been changed. This is a breaking API change, as any existing code calling this function will fail to compile. The pull request description marks this as a "non-breaking change", which is inaccurate. Please update the PR description to reflect that this is a breaking change.

Comment on lines 109 to +118
gettime(&now, &offs);

if (clk_id == CLOCK_REALTIME)
if (clk_id == CLOCK_REALTIME) {
now += offs;
}
else if (clk_id == CLOCK_THREAD_CPUTIME_ID) {
tid = gettid();
threadsinfo(&tid, PH_THREADINFO_CPUTIME, 1, &info);
now = info.cpuTime;
}
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

high

There are a couple of issues in this block:

  1. The gettime() function is called unconditionally, even for CLOCK_THREAD_CPUTIME_ID where its results are not used. This is inefficient. The call should only be made when needed.
  2. The return value of threadsinfo() is not checked. If it fails, info.cpuTime could contain an uninitialized value, leading to clock_gettime() returning an incorrect time with a success status (EOK). The return value should be checked for errors.

Here's a suggested refactoring that addresses both points:

	if (clk_id == CLOCK_THREAD_CPUTIME_ID) {
		tid = gettid();
		if (threadsinfo(&tid, PH_THREADINFO_CPUTIME, 1, &info) < 0) {
			return -1; /* Assumes threadsinfo sets errno on failure */
		}
		now = info.cpuTime;
	}
	else {
		gettime(&now, &offs);
		if (clk_id == CLOCK_REALTIME) {
			now += offs;
		}
	}

Comment on lines +104 to 107
if (clk_id != CLOCK_REALTIME && clk_id != CLOCK_MONOTONIC && clk_id != CLOCK_MONOTONIC_RAW && clk_id != CLOCK_THREAD_CPUTIME_ID) {
errno = EINVAL;
return -1;
}
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

This chain of && conditions to validate clk_id is becoming long and can be hard to read. Using a switch statement would be more readable and maintainable, especially if more clock IDs are added in the future.

	switch (clk_id) {
	case CLOCK_REALTIME:
	case CLOCK_MONOTONIC:
	case CLOCK_MONOTONIC_RAW:
	case CLOCK_THREAD_CPUTIME_ID:
		break;
	default:
		errno = EINVAL;
		return -1;
	}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant