@@ -67,14 +67,6 @@ static const struct k_clock clock_realtime, clock_monotonic;
67
67
* to implement others. This structure defines the various
68
68
* clocks.
69
69
*
70
- * RESOLUTION: Clock resolution is used to round up timer and interval
71
- * times, NOT to report clock times, which are reported with as
72
- * much resolution as the system can muster. In some cases this
73
- * resolution may depend on the underlying clock hardware and
74
- * may not be quantifiable until run time, and only then is the
75
- * necessary code is written. The standard says we should say
76
- * something about this issue in the documentation...
77
- *
78
70
* FUNCTIONS: The CLOCKs structure defines possible functions to
79
71
* handle various clock functions.
80
72
*
@@ -1198,6 +1190,79 @@ SYSCALL_DEFINE2(clock_adjtime, const clockid_t, which_clock,
1198
1190
return err ;
1199
1191
}
1200
1192
1193
+ /**
1194
+ * sys_clock_getres - Get the resolution of a clock
1195
+ * @which_clock: The clock to get the resolution for
1196
+ * @tp: Pointer to a a user space timespec64 for storage
1197
+ *
1198
+ * POSIX defines:
1199
+ *
1200
+ * "The clock_getres() function shall return the resolution of any
1201
+ * clock. Clock resolutions are implementation-defined and cannot be set by
1202
+ * a process. If the argument res is not NULL, the resolution of the
1203
+ * specified clock shall be stored in the location pointed to by res. If
1204
+ * res is NULL, the clock resolution is not returned. If the time argument
1205
+ * of clock_settime() is not a multiple of res, then the value is truncated
1206
+ * to a multiple of res."
1207
+ *
1208
+ * Due to the various hardware constraints the real resolution can vary
1209
+ * wildly and even change during runtime when the underlying devices are
1210
+ * replaced. The kernel also can use hardware devices with different
1211
+ * resolutions for reading the time and for arming timers.
1212
+ *
1213
+ * The kernel therefore deviates from the POSIX spec in various aspects:
1214
+ *
1215
+ * 1) The resolution returned to user space
1216
+ *
1217
+ * For CLOCK_REALTIME, CLOCK_MONOTONIC, CLOCK_BOOTTIME, CLOCK_TAI,
1218
+ * CLOCK_REALTIME_ALARM, CLOCK_BOOTTIME_ALAREM and CLOCK_MONOTONIC_RAW
1219
+ * the kernel differentiates only two cases:
1220
+ *
1221
+ * I) Low resolution mode:
1222
+ *
1223
+ * When high resolution timers are disabled at compile or runtime
1224
+ * the resolution returned is nanoseconds per tick, which represents
1225
+ * the precision at which timers expire.
1226
+ *
1227
+ * II) High resolution mode:
1228
+ *
1229
+ * When high resolution timers are enabled the resolution returned
1230
+ * is always one nanosecond independent of the actual resolution of
1231
+ * the underlying hardware devices.
1232
+ *
1233
+ * For CLOCK_*_ALARM the actual resolution depends on system
1234
+ * state. When system is running the resolution is the same as the
1235
+ * resolution of the other clocks. During suspend the actual
1236
+ * resolution is the resolution of the underlying RTC device which
1237
+ * might be way less precise than the clockevent device used during
1238
+ * running state.
1239
+ *
1240
+ * For CLOCK_REALTIME_COARSE and CLOCK_MONOTONIC_COARSE the resolution
1241
+ * returned is always nanoseconds per tick.
1242
+ *
1243
+ * For CLOCK_PROCESS_CPUTIME and CLOCK_THREAD_CPUTIME the resolution
1244
+ * returned is always one nanosecond under the assumption that the
1245
+ * underlying scheduler clock has a better resolution than nanoseconds
1246
+ * per tick.
1247
+ *
1248
+ * For dynamic POSIX clocks (PTP devices) the resolution returned is
1249
+ * always one nanosecond.
1250
+ *
1251
+ * 2) Affect on sys_clock_settime()
1252
+ *
1253
+ * The kernel does not truncate the time which is handed in to
1254
+ * sys_clock_settime(). The kernel internal timekeeping is always using
1255
+ * nanoseconds precision independent of the clocksource device which is
1256
+ * used to read the time from. The resolution of that device only
1257
+ * affects the presicion of the time returned by sys_clock_gettime().
1258
+ *
1259
+ * Returns:
1260
+ * 0 Success. @tp contains the resolution
1261
+ * -EINVAL @which_clock is not a valid clock ID
1262
+ * -EFAULT Copying the resolution to @tp faulted
1263
+ * -ENODEV Dynamic POSIX clock is not backed by a device
1264
+ * -EOPNOTSUPP Dynamic POSIX clock does not support getres()
1265
+ */
1201
1266
SYSCALL_DEFINE2 (clock_getres , const clockid_t , which_clock ,
1202
1267
struct __kernel_timespec __user * , tp )
1203
1268
{
0 commit comments