1

I need to obtain the point in time (or difference from the epoch, and the epoch) in which the current process started. It has to be in high resolution - in micro-seconds (10^-6 seconds) at least. I'm basically interested in a Linux environment, although a general answer would be useful for other people I suppose.

I know that other programming languages (e.g. C#, Python) have certain facilities for this; from leafing through information about the standard libraries in C, it looks like they don't.

I also know that /proc/self/stat has an uptime figure - but that's in jiffy, and a jiffy is a whole lot of time. Also, I would rather not need to start synchronizing the timing of different system/library calls (one for the uptime, another for the current time).

Note: A related question, start time of a process on linux, regards the start time in seconds, while I need something with much higher resolution.

einpoklum
  • 118,144
  • 57
  • 340
  • 684
  • What is 'Process start time'? Is it when the user clicks on some icon or hits 'Enter' on some command-line? Is it when the OS loader calls 'open' on the executable file to read the header? Is it when the first working-set is loaded into virtual memory? Is it when the loader has started, or finished, creating a thread to run the code? Is it when that thread jumps/calls the start address from the header? Is it when the crt calls main()? All these will have different times at high precision. – Martin James Apr 11 '15 at 09:40

1 Answers1

2

I suspect what you find in /proc/self/stat is the best you can possibly get, that's probably all the OS itself knows about the start time after the fact. Maybe what you have in C# and Python just converts that to microseconds for your convenience.

einpoklum
  • 118,144
  • 57
  • 340
  • 684
Bjorn Munch
  • 496
  • 2
  • 6
  • But if you can get the current time in usec (or better), why would the OS not save something like that as the process start time? – einpoklum Apr 11 '15 at 08:33
  • 1
    Maybe because processes can't actually *start* at sub-jiffy time intervals, so it would serve no purpose to store any higher resolution. You can always convert to microseconds. – Bjorn Munch Apr 11 '15 at 08:49
  • Hmmm... interesting. I wonder if that would work (I need it to convert profiler results whose time values are relative to the process start time). But, you said "I suspect"... are you sure? – einpoklum Apr 11 '15 at 09:13
  • No I'm not sure but I think that entry in /proc/self/stat is the best you can get, see also the other comment above. Starting of a process is not something that happens instantaneously, so microsecond precision is not really meaningful. And if you mean the first instance the CPU executes code in the context of that PID, that's not something that happens at arbitrary times either, only when the OS does a context switch. If you need anything more precise, you may have to register the exact time yourself, as the first thing in main(). – Bjorn Munch Apr 11 '15 at 10:33