【发布时间】:2022-01-07 07:18:50
【问题描述】:
在我的 iOS 项目中,能够复制 Combine's Schedulers 实现,我们进行了广泛的测试,在英特尔机器上一切正常,所有测试都通过了,现在我们得到了一些 M1 机器,看看是否有我们工作流程中的佼佼者。
我们的一些库代码突然开始失败,奇怪的是即使我们使用 Combine 的实现,测试仍然失败。
我们的假设是我们在滥用DispatchTime(uptimeNanoseconds:),正如您在以下屏幕截图(Combine 的实现)中看到的那样
根据文档,我们现在知道使用 uptimeNanoseconds 值初始化 DispatchTime 并不意味着它们是 M1 机器上的实际纳秒数
创建一个相对于系统时钟的
DispatchTime自启动以来的滴答声。
- Parameters:
- uptimeNanoseconds: The number of nanoseconds since boot, excluding
time the system spent asleep
- Returns: A new `DispatchTime`
- Discussion: This clock is the same as the value returned by
`mach_absolute_time` when converted into nanoseconds.
On some platforms, the nanosecond value is rounded up to a
multiple of the Mach timebase, using the conversion factors
returned by `mach_timebase_info()`. The nanosecond equivalent
of the rounded result can be obtained by reading the
`uptimeNanoseconds` property.
Note that `DispatchTime(uptimeNanoseconds: 0)` is
equivalent to `DispatchTime.now()`, that is, its value
represents the number of nanoseconds since boot (excluding
system sleep time), not zero nanoseconds since boot.
那么,是测试错误还是我们不应该这样使用DispatchTime?
我们尝试关注Apple suggestion 并使用它:
uint64_t MachTimeToNanoseconds(uint64_t machTime)
{
uint64_t nanoseconds = 0;
static mach_timebase_info_data_t sTimebase;
if (sTimebase.denom == 0)
(void)mach_timebase_info(&sTimebase);
nanoseconds = ((machTime * sTimebase.numer) / sTimebase.denom);
return nanoseconds;
}
没有太大帮助。
编辑:截图代码:
func testSchedulerTimeTypeDistance() {
let time1 = DispatchQueue.SchedulerTimeType(.init(uptimeNanoseconds: 10000))
let time2 = DispatchQueue.SchedulerTimeType(.init(uptimeNanoseconds: 10431))
let distantFuture = DispatchQueue.SchedulerTimeType(.distantFuture)
let notSoDistantFuture = DispatchQueue.SchedulerTimeType(
DispatchTime(
uptimeNanoseconds: DispatchTime.distantFuture.uptimeNanoseconds - 1024
)
)
XCTAssertEqual(time1.distance(to: time2), .nanoseconds(431))
XCTAssertEqual(time2.distance(to: time1), .nanoseconds(-431))
XCTAssertEqual(time1.distance(to: distantFuture), .nanoseconds(-10001))
XCTAssertEqual(distantFuture.distance(to: time1), .nanoseconds(10001))
XCTAssertEqual(time2.distance(to: distantFuture), .nanoseconds(-10432))
XCTAssertEqual(distantFuture.distance(to: time2), .nanoseconds(10432))
XCTAssertEqual(time1.distance(to: notSoDistantFuture), .nanoseconds(-11025))
XCTAssertEqual(notSoDistantFuture.distance(to: time1), .nanoseconds(11025))
XCTAssertEqual(time2.distance(to: notSoDistantFuture), .nanoseconds(-11456))
XCTAssertEqual(notSoDistantFuture.distance(to: time2), .nanoseconds(11456))
XCTAssertEqual(distantFuture.distance(to: distantFuture), .nanoseconds(0))
XCTAssertEqual(notSoDistantFuture.distance(to: notSoDistantFuture),
.nanoseconds(0))
}
【问题讨论】:
-
我之前在
htop工作过,它使用了这个功能。比较可能有用:github.com/htop-dev/htop/blob/… -
最好不要将重要信息作为图像包含,您可以复制/粘贴相关文本吗?
标签: swift macos apple-m1 arm64