2 * Copyright (c) 2022 Samsung Electronics Co., Ltd.
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
21 static const int TIMEOUT_MS = 1000;
23 /* We allow the maximum difference of 5 seconds between correct and timestamp time.
24 * This is a window big enough to make false negatives almost impossible.
25 * The time skewing by less than 5 seconds consistently is possible, but improbable. */
26 static const int TIMESTAMP_WINDOW_S = 5;
28 bool are_timestamps_close(struct timespec a, struct timespec b) {
29 long long nsec_diff = (long long)a.tv_nsec - (long long)b.tv_nsec;
30 int sec_diff = (int)a.tv_sec - (int)b.tv_sec;
33 else if (nsec_diff > 0)
35 return abs(sec_diff) <= TIMESTAMP_WINDOW_S;
39 struct timespec real_goal, mono_goal;
40 assert(clock_gettime(CLOCK_REALTIME, &real_goal) == 0);
41 assert(clock_gettime(CLOCK_MONOTONIC, &mono_goal) == 0);
43 dlogutil_config_s *c = dlogutil_config_create();
46 assert(dlogutil_config_buffer_add(c, LOG_ID_MAIN) == 0);
47 assert(dlogutil_config_mode_set_continuous(c) == 0);
49 dlogutil_state_s *s = NULL;
50 assert(dlogutil_config_connect(c, &s) == 0);
54 assert(dlogutil_get_log(s, TIMEOUT_MS, &e) == 0);
56 struct timespec scratch;
57 if (dlogutil_entry_get_timestamp(e, DLOGUTIL_SORT_RECV_REAL, &scratch) == 0)
58 assert(are_timestamps_close(real_goal, scratch));
59 if (dlogutil_entry_get_timestamp(e, DLOGUTIL_SORT_SENT_REAL, &scratch) == 0)
60 assert(are_timestamps_close(real_goal, scratch));
61 if (dlogutil_entry_get_timestamp(e, DLOGUTIL_SORT_RECV_MONO, &scratch) == 0)
62 assert(are_timestamps_close(mono_goal, scratch));
63 if (dlogutil_entry_get_timestamp(e, DLOGUTIL_SORT_SENT_MONO, &scratch) == 0)
64 assert(are_timestamps_close(mono_goal, scratch));
67 dlogutil_state_destroy(s);
68 dlogutil_config_destroy(c);