libs-team icon indicating copy to clipboard operation
libs-team copied to clipboard

Feature: A separate type for measuring thread time in std::time module

Open artech-git opened this issue 1 year ago • 5 comments

Had this thought regarding if we can infer the time it takes for object to be executed in a given OS thread context, instead of relying on the std::time::Instant to obtain the time elapsed which won't take into account things such as context switch etc etc, therefore I would like to propose a thought ThreadTime a type which may keep track of the time spent by itself on a given thread context excluding all the context switching mechanism..

internally it may look like so

pub struct ThreadTime { 
    duration: ThreadDuration, 
    thread: SomeThreadTypeRecorder
}

through this we can avoid using ThreadLocal<T> types directly which may wrap duration between itself, and then maybe we continue the computation, this can be especially useful if you wish to use it in the some struct where you wish to track time in precision. and should have !Sync enabled, let me know is it a useful idea ?

Iam not sure though whether this can be made platform agnostic ? and also O.S agnostic too.

artech-git avatar Mar 01 '24 21:03 artech-git

I'm not sure I understand the use case for something like this, nor do I understand the mechanism for which something like this would be implemented. Can you flesh both of those things out a bit more please? Real world examples would help. It would also help to describe why a thread local is an inappropriate solution.

BurntSushi avatar Mar 02 '24 01:03 BurntSushi

I assume you mean something like CLOCK_THREAD_CPUTIME_ID? This is definitely possible to implement, at least on UNIX. Do you have a concrete problem in mind that you would like to use this for?

joboet avatar Mar 02 '24 10:03 joboet

There's the cpu-time crate and possibly others.

What would be the value of having this in std instead of a crate?

the8472 avatar Mar 02 '24 10:03 the8472