Bug 243810 - Watchdog can't work on Mac with m1 chip because wrong thread cpu time.
Summary: Watchdog can't work on Mac with m1 chip because wrong thread cpu time.
Status: NEW
Alias: None
Product: WebKit
Classification: Unclassified
Component: JavaScriptCore (show other bugs)
Version: Other
Hardware: Mac (Apple Silicon) macOS 12
: P2 Normal
Assignee: Nobody
URL:
Keywords: InRadar
Depends on:
Blocks:
 
Reported: 2022-08-10 20:28 PDT by Vincent.yu
Modified: 2022-08-17 20:29 PDT (History)
4 users (show)

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Vincent.yu 2022-08-10 20:28:26 PDT
Watchdog.cpp use CPUTime::forCurrentThread, which use clock_gettime(CLOCK_THREAD_CPUTIME_ID, &ts), to get the cpu clock of thread. But on Mac with m1, the clock_gettime(CLOCK_THREAD_CPUTIME_ID) is always incredibly small.


Here is the test code:

```
double CPUTime()
{
    struct timespec ts;
    int ret = clock_gettime(CLOCK_THREAD_CPUTIME_ID, &ts);
    return ts.tv_sec + ts.tv_nsec/1000.0/1000.0/1000.0;
}

double RealTime()
{
    struct timespec ts;
    int ret = clock_gettime(CLOCK_MONOTONIC, &ts);
    return ts.tv_sec + ts.tv_nsec/1000.0/1000.0/1000.0;
}

void longTask()
{
    double begin = RealTime();
    double limit = 1.0;
    static double g_result = 0;
    while (1) {
        for (int i=0; i<1000000; i++) {
            g_result = sin(g_result+5);
        }
        double now = RealTime();
        if (now - begin >= limit) break;
    }
    return;
}

void testClocks()
{
    double t1, t0, cpuTimeCost, realTimeCost;
    
    t0 = CPUTime();
    longTask();
    t1 = CPUTime();
    cpuTimeCost = t1 - t0;
    
    double t10 = RealTime();
    longTask();
    double t11 = RealTime();
    realTimeCost = t11 - t10;
    NSLog(@"\nthread cpu: %lfs\nreal: %lfs", cpuTimeCost, realTimeCost);
    return;
}
```

It will output: 

```
thread cpu: 0.024395s
real: 1.006627s
```

Only the M1 chip has this problem. On Mac with Intel, these two clocks are almost the same.
Comment 1 Radar WebKit Bug Importer 2022-08-17 20:29:17 PDT
<rdar://problem/98812069>