1 /*
2 * Copyright (C) 2021 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16 #include "test_timer.h"
17
18 #include <stdlib.h>
19
20 #ifdef __cplusplus
21 extern "C" {
22 #endif
23
24 // 3861 platform limits that the running time cannot exceed 16 seconds.
25 // If the running time exceeds 16 seconds, crash occurs.
26 // The following comments are from at91SAM9_wdt.c in directory
27 // device/hisilicon/hispark_pegasus/sdk_liteos/third_party/u-boot-v2019.07/u-boot-v2019.07/drivers/watchdog
28 /*
29 * All counting occurs at SLOW_CLOCK / 128 = 256 Hz
30 *
31 * Since WDV is a 12-bit counter, the maximum period is
32 * 4096 / 256 = 16 seconds.
33 */
34
35 enum {
36 MAX_FIBONACCI_OPTION = 0xFFFFFF,
37 MIN_FIBONACCI_OPTION = 0xFFFFF,
38 };
39
CalcTimeSpecDiff(struct timespec before,struct timespec after)40 double CalcTimeSpecDiff(struct timespec before, struct timespec after)
41 {
42 double mseconds = (((double)after.tv_sec - (double)before.tv_sec) * 1000000.0f +
43 ((double)after.tv_nsec - (double)before.tv_nsec) / 1000.0f) / 1000.0f;
44 return mseconds;
45 }
46
TimeConsumingOperation(void)47 unsigned long long TimeConsumingOperation(void)
48 {
49 unsigned long long i, n, t1 = 0, t2 = 1, nextTerm = 0;
50 n = rand() % (MAX_FIBONACCI_OPTION - MIN_FIBONACCI_OPTION) + MIN_FIBONACCI_OPTION;
51 for (i = 1; i <= n; ++i) {
52 nextTerm = t1 + t2;
53 t1 = t2;
54 t2 = nextTerm;
55 }
56
57 return nextTerm;
58 }
59
60 #ifdef __cplusplus
61 }
62 #endif
63