Skip to content

On Linux, System.Diagnostics.Process.StartTime is not stable #108959

Open

Description

@cklutz

Description

It looks like, that the way that System.Diagnostics.Process.StartTime is calculated is not stable. That is, if you get the start time multiple times, you get different values - even if just in the millisecond range.

This is a problem, not only for precision in general, but also for applications that use a process' start time together with its process ID to uniquely identify it. Also, Windows doesn't have this problem, making porting and compatibility tricky. Finally, the current behavior does not allow other code to reproduce the exact same start time that the CLR calculates (something which, again, is possible on Windows). There might also be issues comparing this start time with the one reported by other (native) tools. Again, which might cause "identity problems".

Reproduction Steps

Consider the following Program:

using System.Diagnostics;
using System;

public static class Program
{
        public static void Main(string[] args)
        {
                using var process = Process.GetProcessById(Int32.Parse(args[0]));
                Console.WriteLine(process.StartTime.ToString("O"));
        }
}

On a Linux system invoke it multiple times (e.g. for the init process, PID 1)

$ dotnet bin/Debug/net8.0/starttime 1

Expected behavior

Multiple invocations produce the exact same start time for the target process.

Actual behavior

Multiple invocations produce different start times for the target process (actual output):

$ dotnet ./bin/Debug/net8.0/starttime.dll 1
2024-10-17T03:12:50.9611154+02:00
$ dotnet ./bin/Debug/net8.0/starttime.dll 1
2024-10-17T03:12:50.9520283+02:00
$ dotnet ./bin/Debug/net8.0/starttime.dll 1
2024-10-17T03:12:50.9505063+02:00

Regression?

No response

Known Workarounds

Not a workaround, but it kind of "works" if you only ever use such timestamps from within the same process, because inside Process.Linux.cs, the boot time is cached (in s_bootTimeTicks).

Configuration

  • .NET 8.0
  • Ubuntu 23.04, x64
  • WSL 2

Other information

It looks like it is actually an issue in the implementation of the native SystemNative_GetBootTimeTicks function in pal_time.c. It uses CLOCK_REALTIME_COARSE to calculate part of the time stamp and that returns unstable (or "less precise") values.

If I understand the rest of the code to get Process.StartTime correctly, all other values (like starttime from the process' procfs-stat file) are basically constants. So the observed behavior should be related to the native code.

Consider the following "low-level" repro (code copied from pal_time.c):

/* build: clang foo.cpp */
#include<time.h>
#include<stdint.h>
#include<assert.h>
#include<unistd.h>
#include<string.h>

enum
{
    MicroSecondsToNanoSeconds = 1000,   // 10^3
    SecondsToNanoSeconds = 1000000000,  // 10^9
    SecondsToTicks = 10000000,          // 10^7
    TicksToNanoSeconds = 100,           // 10^2
};

const int64_t UnixEpochTicks = 621355968000000000;

int main(int argc, char** argv) {
        int64_t previous = 0;
        for (int i = 0; i < 10; i++) {
                struct timespec ts;
                int result = clock_gettime(CLOCK_BOOTTIME, &ts);
                assert(result == 0);

                int64_t sinceBootTicks = ((int64_t)ts.tv_sec * SecondsToTicks) + (ts.tv_nsec / TicksToNanoSeconds);

                if (strcmp(argv[1], "--mono") == 0) {
                        result = clock_gettime(CLOCK_MONOTONIC, &ts);
                } else {
                        result = clock_gettime(CLOCK_REALTIME_COARSE, &ts);
                }
                assert(result == 0);

                int64_t sinceEpochTicks = ((int64_t)ts.tv_sec * SecondsToTicks) + (ts.tv_nsec / TicksToNanoSeconds);

                int64_t bootTime = UnixEpochTicks + sinceEpochTicks - sinceBootTicks;

                if (i > 0) {
                        printf("sinceBootTicks: %ld, sinceEpochTicks: %ld, bootTime: %ld, delta: %ld\n",
                                sinceBootTicks, sinceEpochTicks, bootTime, bootTime - previous);
                } else {
                        printf("sinceBootTicks: %ld, sinceEpochTicks: %ld, bootTime: %ld\n",
                                sinceBootTicks, sinceEpochTicks, bootTime);
                }
                previous = bootTime;
                sleep(1);
        }
        return 0;
}

If you compile and invoke the resulting binary with --mono it will use CLOCK_REALTIME_COARSE rather than CLOCK_REALTIME_COARSE and will produce stable results:

./a.out --coarse
sinceBootTicks: 230188979998, sinceEpochTicks: 17291505892061801, bootTime: 638647243703081803
sinceBootTicks: 230199052191, sinceEpochTicks: 17291505902161801, bootTime: 638647243703109610, delta: 27807
sinceBootTicks: 230209124116, sinceEpochTicks: 17291505912261800, bootTime: 638647243703137684, delta: 28074
sinceBootTicks: 230219196751, sinceEpochTicks: 17291505922161794, bootTime: 638647243702965043, delta: -172641
sinceBootTicks: 230229269369, sinceEpochTicks: 17291505932361787, bootTime: 638647243703092418, delta: 127375
sinceBootTicks: 230239345412, sinceEpochTicks: 17291505942461781, bootTime: 638647243703116369, delta: 23951
sinceBootTicks: 230249416194, sinceEpochTicks: 17291505952461774, bootTime: 638647243703045580, delta: -70789

Compare with

./a.out --mono
sinceBootTicks: 229996721520, sinceEpochTicks: 229996721534, bootTime: 621355968000000014
sinceBootTicks: 230006794304, sinceEpochTicks: 230006794308, bootTime: 621355968000000004, delta: -10
sinceBootTicks: 230016866885, sinceEpochTicks: 230016866889, bootTime: 621355968000000004, delta: 0
sinceBootTicks: 230026938539, sinceEpochTicks: 230026938543, bootTime: 621355968000000004, delta: 0
sinceBootTicks: 230037009285, sinceEpochTicks: 230037009294, bootTime: 621355968000000009, delta: 5
sinceBootTicks: 230047080913, sinceEpochTicks: 230047080917, bootTime: 621355968000000004, delta: -5
sinceBootTicks: 230057096215, sinceEpochTicks: 230057096219, bootTime: 621355968000000004, delta: 0

As you can see, the difference (in ticks) between the individual calculations is much smaller.
Albeit still not 100% identical. So I'm not sure if that would really be a fix - given nothing
else really requires CLOCK_REALTIME_COARSE here.

Activity

  1. ghost added
    needs-area-label
    on Oct 17, 2024
  2. added
    untriaged
    on Oct 17, 2024
  3. added and removed
    needs-area-label
    on Oct 17, 2024
  4. dotnet-policy-service commented on Oct 17, 2024

    @dotnet-policy-service
    Contributor

    Tagging subscribers to this area: @dotnet/area-system-diagnostics-process
    See info in area-owners.md if you want to be subscribed.

  5. added
    help wanted
    needs-further-triage
    and removed
    untriaged
    on Oct 20, 2024
  6. added this to the Future milestone on Oct 20, 2024
  7. cklutz commented on Oct 24, 2024

    @cklutz
    Author

    I'd like to add some thoughts (unfortunately, I don't think I have enough experience to propose/code a proper "fix" for the issue myself).

    When using the CLOCK_MONOTONIC or CLOCK_REALTIME (not CLOCK_REALTIME_COARSE) the values seem to be a lot less varying. But as can be seen above, even then they do differ (when they probably shouldn't). This might be an artefact due to running in WSL, but nevertheless the problem shouldn't be one even there.

    Maybe it would be sufficient to ensure that values are precise enough up to the millisecond (or maybe even only 10th of milliseconds). Thus effectively hiding the differences that may not be preventable.

    But that would require that all smaller fractions are zeroed out by Process.StartTime. For example, instead of returning the "measured" value 2024-10-17T03:12:50.9515063+02:00 return 2024-10-17T03:12:50.9510000+02:00 or even 2024-10-17T03:12:50.9500000+02:00.

    Given typical PID recycling that shouldn't lead to ambiguity regarding process identification with (ProcessId, StartTime).

    Applications could do this themselves, but than cannot be sure that other code (libraries, packages, etc.) would do it the same. Also multi-platform apps would have to do this only for, say, Linux but not for Windows.

    Also, should there be a way to determine the start time with higher precision in the future (or on different platforms, runtime environments), the accuracy could be increased without breaking existing code.

    I'm sure there are quite some issues with the idea above, but I wanted to toss it out anyway ;-)

  8. Neo-vortex commented on Nov 24, 2025

    @Neo-vortex
    Contributor

    I can confirm this on physical Linux system as well (Ubuntu 24.01 X64)

    This benchmark is specifically focusing on clock jitter. this test determines how much the offset between two independent system clocks varies over time by repeatedly calculating the difference: Offset= CLOCK_REALTIME (CLOCK_REALTIME_COARSE) - CLOCK_BOOTTIME. Since this difference should ideally be constant, any variation (measured as Peak-to-Peak) is Jitter and will cause inconsistent results.

    #include <stdio.h>
    #include <stdlib.h>
    #include <time.h>
    #include <unistd.h>
    #include <math.h>
    #include <string.h>
    
    #define NUM_TRIALS 100        
    #define CALLS_PER_TRIAL 1000   
    
    #define NSEC_PER_SEC 10000000LL
    #define NSEC_PER_TICK 100LL  
    
    long long timespec_to_ns(const struct timespec *ts) {
        return (long long)ts->tv_sec * NSEC_PER_SEC + ts->tv_nsec;
    }
    
    long long ns_to_ticks(long long ns) {
        return ns / NSEC_PER_TICK;
    }
    
    long long get_boot_ticks_fine() {
        struct timespec realtime, boottime;
    
        if (clock_gettime(CLOCK_REALTIME, &realtime) != 0) return -1;
        if (clock_gettime(CLOCK_BOOTTIME, &boottime) != 0) return -1;
    
        long long real_ns = timespec_to_ns(&realtime);
        long long boot_ns = timespec_to_ns(&boottime);
        return ns_to_ticks(real_ns - boot_ns);
    }
    
    long long get_boot_ticks_coarse() {
        struct timespec realtime_coarse, boottime_coarse;
    
        if (clock_gettime(CLOCK_REALTIME_COARSE, &realtime_coarse) != 0) return -1;
        if (clock_gettime(CLOCK_BOOTTIME, &boottime_coarse) != 0) return -1;
    
        long long real_ns = timespec_to_ns(&realtime_coarse);
        long long boot_ns = timespec_to_ns(&boottime_coarse);
        return ns_to_ticks(real_ns - boot_ns);
    }
    
    typedef struct {
        long long min_value;
        long long max_value;
        double mean;
        double std_dev; 
    } AnalysisResult;
    
    AnalysisResult analyze_results(long long *data, int n) {
        AnalysisResult res = { 
            .min_value = data[0], 
            .max_value = data[0], 
            .mean = 0.0, 
            .std_dev = 0.0 
        };
        double sum = 0.0;
    
        for (int i = 0; i < n; i++) {
            long long val = data[i];
            if (val < res.min_value) res.min_value = val;
            if (val > res.max_value) res.max_value = val;
            sum += val;
        }
    
        res.mean = sum / n;
    
        double sum_sq_diff = 0.0;
        for (int i = 0; i < n; i++) {
            sum_sq_diff += pow((double)data[i] - res.mean, 2);
        }
        res.std_dev = sqrt(sum_sq_diff / n);
    
        return res;
    }
    
    void run_benchmark(long long (*func)(void), long long *results_array) {
        
        
        for (int t = 0; t < NUM_TRIALS; t++) {
            
            long long final_offset_value = 0;
            for (int c = 0; c < CALLS_PER_TRIAL; c++) {
                final_offset_value = func();
            }
            results_array[t] = final_offset_value;
    
            
        }
    }
    
    // --- Conversion Macros ---
    #define TICKS_TO_NS(ticks) ((double)(ticks) * NSEC_PER_TICK)
    #define TICKS_TO_US(ticks) ((double)(ticks) * NSEC_PER_TICK / 1000.0)
    #define TICKS_TO_MS(ticks) ((double)(ticks) * NSEC_PER_TICK / 1000000.0)
    
    // --- Main Execution ---
    
    int main() {
        printf("--- Clock Offset Stability Benchmark (Consecutive Trials) ---\n");
        printf("Goal: Measure the consistency (jitter) of the calculated clock offset.\n");
        printf("Settings: %d trials, %d calls per trial (run back-to-back).\n", NUM_TRIALS, CALLS_PER_TRIAL);
        printf("Offset unit: 1 tick = %lld nanoseconds\n\n", NSEC_PER_TICK);
        
        long long fine_results[NUM_TRIALS];
        long long coarse_results[NUM_TRIALS];
    
        printf("Running Fine Clock Benchmark (CLOCK_REALTIME/CLOCK_BOOTTIME)...\n");
        run_benchmark(get_boot_ticks_fine, fine_results);
        AnalysisResult fine_analysis = analyze_results(fine_results, NUM_TRIALS);
    
        printf("Running Coarse Clock Benchmark (CLOCK_REALTIME_COARSE/CLOCK_MONOTONIC_COARSE)...\n");
        run_benchmark(get_boot_ticks_coarse, coarse_results);
        AnalysisResult coarse_analysis = analyze_results(coarse_results, NUM_TRIALS);
    
        printf("\n--- RESULTS: Stability of Clock Offset ---\n");
    
        long long fine_jitter_ticks = fine_analysis.max_value - fine_analysis.min_value;
        long long coarse_jitter_ticks = coarse_analysis.max_value - coarse_analysis.min_value;
    
        // FINE Results
        printf("\n[Fine Clock Offset (High-Resolution)]\n");
        printf("  Mean Calculated Offset: %.0f ticks\n", fine_analysis.mean);
        
        printf("  Observed Range (Peak-to-Peak Jitter):\n");
        printf("    Min Offset: %lld ticks\n", fine_analysis.min_value);
        printf("    Max Offset: %lld ticks\n", fine_analysis.max_value);
        
        // --- FINE: PEAK-TO-PEAK JITTER DURATION (Max - Min) ---
        printf("  Total Peak-to-Peak Jitter:\n");
        printf("    In Ticks: %lld ticks\n", fine_jitter_ticks);
        printf("    In Time:  %.0f ns / %.2f us / %.3f ms\n",
               TICKS_TO_NS(fine_jitter_ticks),
               TICKS_TO_US(fine_jitter_ticks),
               TICKS_TO_MS(fine_jitter_ticks));
            
    
    
        // COARSE Results
        printf("\n[Coarse Clock Offset (Low-Resolution)]\n");
        printf("  Mean Calculated Offset: %.0f ticks\n", coarse_analysis.mean);
        
        printf("  Observed Range (Peak-to-Peak Jitter):\n");
        printf("    Min Offset: %lld ticks\n", coarse_analysis.min_value);
        printf("    Max Offset: %lld ticks\n", coarse_analysis.max_value);
        
        // --- COARSE: PEAK-TO-PEAK JITTER DURATION (Max - Min) ---
        printf("  Total Peak-to-Peak Jitter:\n");
        printf("    In Ticks: %lld ticks\n", coarse_jitter_ticks);
        printf("    In Time:  %.0f ns / %.2f us / %.3f ms\n",
               TICKS_TO_NS(coarse_jitter_ticks),
               TICKS_TO_US(coarse_jitter_ticks),
               TICKS_TO_MS(coarse_jitter_ticks));
            
    
        return 0;
    }

    Shows this result :

    Settings: 100 trials, 1000 calls per trial (run back-to-back).
    Offset unit: 1 tick = 100 nanoseconds
    
    Running Fine Clock Benchmark (CLOCK_REALTIME/CLOCK_BOOTTIME)...
    Running Coarse Clock Benchmark (CLOCK_REALTIME_COARSE/CLOCK_MONOTONIC_COARSE)...
    
    --- RESULTS: Stability of Clock Offset ---
    
    [Fine Clock Offset (High-Resolution)]
      Mean Calculated Offset: 176397190940062 ticks
      Observed Range (Peak-to-Peak Jitter):
        Min Offset: 176397190940062 ticks
        Max Offset: 176397190940062 ticks
      Total Peak-to-Peak Jitter:
        In Ticks: 0 ticks
        In Time:  0 ns / 0.00 us / 0.000 ms
    
    [Coarse Clock Offset (Low-Resolution)]
      Mean Calculated Offset: 176397190925664 ticks
      Observed Range (Peak-to-Peak Jitter):
        Min Offset: 176397190921175 ticks
        Max Offset: 176397190930953 ticks
      Total Peak-to-Peak Jitter:
        In Ticks: 9778 ticks
        In Time:  977800 ns / 977.80 us / 0.978 ms
    

    Results show CLOCK_REALTIME_COARSE off by up to 1 ms versus CLOCK_REALTIME being super stable.

    Propose using CLOCK_REALTIME instead of CLOCK_REALTIME_COARSE. Call overhead is negligible.

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    area-System.Diagnostics.Processhelp wantedneeds-further-triage

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions