1 /*
<lambda>null2 * Copyright (C) 2023 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17 package android.tools.traces.parsers.perfetto
18
19 fun queryRealToMonotonicTimeOffsetNs(session: TraceProcessorSession, tableName: String): Long {
20 val elapsed = queryLastEntryTimestamp(session, tableName)
21 if (elapsed == null) {
22 return 0L
23 }
24 val monotonic = queryToMonotonic(session, elapsed)
25 val real = queryToRealtime(session, elapsed)
26 return real - monotonic
27 }
28
queryRealToElapsedTimeOffsetNsnull29 fun queryRealToElapsedTimeOffsetNs(session: TraceProcessorSession, tableName: String): Long {
30 val elapsed = queryLastEntryTimestamp(session, tableName) ?: return 0L
31 val real = queryToRealtime(session, elapsed)
32 return real - elapsed
33 }
34
queryLastEntryTimestampnull35 fun queryLastEntryTimestamp(session: TraceProcessorSession, tableName: String): Long? {
36 val sql =
37 """
38 SELECT
39 ts
40 FROM $tableName
41 WHERE
42 id = ( SELECT MAX(id) FROM $tableName );
43 """
44 .trimIndent()
45 val value =
46 session.query(sql) { rows ->
47 if (rows.size == 1) {
48 rows.get(0).get("ts") as Long
49 } else {
50 null
51 }
52 }
53 return value
54 }
55
queryToMonotonicnull56 fun queryToMonotonic(session: TraceProcessorSession, elapsedTimestamp: Long): Long {
57 val sql = "SELECT TO_MONOTONIC($elapsedTimestamp) as ts;"
58 val value =
59 session.query(sql) { rows ->
60 require(rows.size == 1)
61 rows.get(0).get("ts") as Long
62 }
63 return value
64 }
65
queryToRealtimenull66 fun queryToRealtime(session: TraceProcessorSession, elapsedTimestamp: Long): Long {
67 val sql = "SELECT TO_REALTIME($elapsedTimestamp) as ts;"
68 val value =
69 session.query(sql) { rows ->
70 require(rows.size == 1)
71 rows.get(0).get("ts") as Long
72 }
73 return value
74 }
75