1 /*
2  * Copyright (c) 2020 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 #include "profiler_module.h"
16 #if IS_ENABLED(JS_PROFILER)
17 namespace OHOS {
18 namespace ACELite {
19 /**
20  * Usage:
21  *     profiler.startTracing(5);
22  *     xxxx
23  *     profiler.stopTracing();
24  *  the parameter of profiler.startTracing must be one of PerformanceTag in js_profile.h
25  */
Init()26 void ProfilerModule::Init()
27 {
28     const char * const startTracingFunc = "startTracing";
29     const char * const stopTracingFunc = "stopTracing";
30     CreateNamedFunction(startTracingFunc, StartTracing);
31     CreateNamedFunction(stopTracingFunc, StopTracing);
32 }
33 
StartTracing(const jerry_value_t func,const jerry_value_t context,const jerry_value_t * args,const jerry_length_t argsNum)34 jerry_value_t ProfilerModule::StartTracing(const jerry_value_t func,
35                                            const jerry_value_t context,
36                                            const jerry_value_t *args,
37                                            const jerry_length_t argsNum)
38 {
39     UNUSED(func);
40     UNUSED(context);
41 #if IS_ENABLED(JS_PROFILER)
42     if ((args == nullptr) || (argsNum < 1)) {
43         return UNDEFINED;
44     }
45 
46     uint8_t tag = (uint8_t)jerry_get_number_value(args[0]);
47     uint8_t component = 0;
48     uint16_t description = 0;
49     const uint8_t leastArgsNum = 2;
50     if (argsNum >= leastArgsNum) {
51         component = (uint8_t)jerry_get_number_value(args[1]);
52     }
53     const uint8_t jsArgsNum = 3;
54     if (argsNum == jsArgsNum) {
55         const uint8_t descriptionIndex = 2;
56         description = (uint16_t)jerry_get_number_value(args[descriptionIndex]);
57     }
58 
59     START_TRACING_WITH_EXTRA_INFO((PerformanceTag)tag, component, description);
60 #endif // ENABLED(JS_PROFILER)
61     return UNDEFINED;
62 }
63 
StopTracing(const jerry_value_t func,const jerry_value_t context,const jerry_value_t * args,const jerry_length_t argsNum)64 jerry_value_t ProfilerModule::StopTracing(const jerry_value_t func,
65                                           const jerry_value_t context,
66                                           const jerry_value_t *args,
67                                           const jerry_length_t argsNum)
68 {
69     UNUSED(func);
70     UNUSED(context);
71     UNUSED(args);
72     UNUSED(argsNum);
73 #if IS_ENABLED(JS_PROFILER)
74     STOP_TRACING();
75 #endif
76     return UNDEFINED;
77 }
78 } // namespace ACELite
79 } // namespace OHOS
80 #endif
81