Skip to content

Commit cb2d2ae

Browse files
committed
[SystemZ][ZOS] Provide CLOCK_MONOTONIC alternative
We need CLOCK_MONOTONIC equivalent implementation for z/OS within libc++. The default implementation is asserting. On z/OS the lack of 'clock_gettime()' and 'time_point()' force us to look for alternatives. The current proposal is to use `gettimeofday()` for CLOCK_MONOTONIC which is also used in CLOCK_REALTIME. This will allow us to skip the assertion with compromised CLOCK_MONOTONIC implementation which will not guarantee to never go back in time because it will use `gettimeofday()` but only when it's set. Is this a good compromise for platforms which does not support monotonic clock? Hopefully this will spark the discussion and agreement how to proceed in this situation. Reviewed By: #libc, ldionne, hubert.reinterpretcast Differential Revision: https://reviews.llvm.org/D93542
1 parent 875f0cb commit cb2d2ae

File tree

3 files changed

+75
-0
lines changed

3 files changed

+75
-0
lines changed

libcxx/include/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,7 @@ set(files
153153
strstream
154154
__support/android/locale_bionic.h
155155
__support/fuchsia/xlocale.h
156+
__support/ibm/gettod_zos.h
156157
__support/ibm/limits.h
157158
__support/ibm/locale_mgmt_aix.h
158159
__support/ibm/nanosleep.h
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
// -*- C++ -*-
2+
//===----------------------------------------------------------------------===//
3+
//
4+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
5+
// See https://llvm.org/LICENSE.txt for license information.
6+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7+
//
8+
//===----------------------------------------------------------------------===//
9+
10+
#ifndef _LIBCPP_SUPPORT_IBM_GETTOD_ZOS_H
11+
#define _LIBCPP_SUPPORT_IBM_GETTOD_ZOS_H
12+
13+
#include <time.h>
14+
15+
static inline int gettimeofdayMonotonic(struct timespec64* Output) {
16+
17+
// The POSIX gettimeofday() function is not available on z/OS. Therefore,
18+
// we will call stcke and other hardware instructions in implement equivalent.
19+
// Note that nanoseconds alone will overflow when reaching new epoch in 2042.
20+
21+
struct _t {
22+
uint64_t Hi;
23+
uint64_t Lo;
24+
};
25+
struct _t Value = {0, 0};
26+
uint64_t CC = 0;
27+
asm(" stcke %0\n"
28+
" ipm %1\n"
29+
" srlg %1,%1,28\n"
30+
: "=m"(Value), "+r"(CC)::);
31+
32+
if (CC != 0) {
33+
errno = EMVSTODNOTSET;
34+
return CC;
35+
}
36+
uint64_t us = (Value.Hi >> 4);
37+
uint64_t ns = ((Value.Hi & 0x0F) << 8) + (Value.Lo >> 56);
38+
ns = (ns * 1000) >> 12;
39+
us = us - 2208988800000000;
40+
41+
register uint64_t DivPair0 asm("r0"); // dividend (upper half), remainder
42+
DivPair0 = 0;
43+
register uint64_t DivPair1 asm("r1"); // dividend (lower half), quotient
44+
DivPair1 = us;
45+
uint64_t Divisor = 1000000;
46+
asm(" dlgr %0,%2" : "+r"(DivPair0), "+r"(DivPair1) : "r"(Divisor) :);
47+
48+
Output->tv_sec = DivPair1;
49+
Output->tv_nsec = DivPair0 * 1000 + ns;
50+
return 0;
51+
}
52+
53+
#endif // _LIBCPP_SUPPORT_IBM_GETTOD_ZOS_H

libcxx/src/chrono.cpp

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,20 @@
66
//
77
//===----------------------------------------------------------------------===//
88

9+
#if defined(__MVS__)
10+
// As part of monotonic clock support on z/OS we need macro _LARGE_TIME_API
11+
// to be defined before any system header to include definition of struct timespec64.
12+
#define _LARGE_TIME_API
13+
#endif
14+
915
#include "chrono"
1016
#include "cerrno" // errno
1117
#include "system_error" // __throw_system_error
18+
19+
#if defined(__MVS__)
20+
#include <__support/ibm/gettod_zos.h> // gettimeofdayMonotonic
21+
#endif
22+
1223
#include <time.h> // clock_gettime and CLOCK_{MONOTONIC,REALTIME,MONOTONIC_RAW}
1324
#include "include/apple_availability.h"
1425

@@ -218,6 +229,16 @@ static steady_clock::time_point __libcpp_steady_clock_now() {
218229
return steady_clock::time_point(steady_clock::duration(dur));
219230
}
220231

232+
#elif defined(__MVS__)
233+
234+
static steady_clock::time_point __libcpp_steady_clock_now() {
235+
struct timespec64 ts;
236+
if (0 != gettimeofdayMonotonic(&ts))
237+
__throw_system_error(errno, "failed to obtain time of day");
238+
239+
return steady_clock::time_point(seconds(ts.tv_sec) + nanoseconds(ts.tv_nsec));
240+
}
241+
221242
#elif defined(CLOCK_MONOTONIC)
222243

223244
static steady_clock::time_point __libcpp_steady_clock_now() {

0 commit comments

Comments
 (0)