• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "caller_stat.h"
17 
18 namespace panda::ecmascript {
Start(PandaRuntimeCallerStat * callerStat,PandaRuntimeTimer * parent)19 void PandaRuntimeTimer::Start(PandaRuntimeCallerStat *callerStat, PandaRuntimeTimer *parent)
20 {
21     parent_ = parent;
22     callerStat_ = callerStat;
23     uint64_t nowTime = Now();
24     if (parent != nullptr) {
25         parent_->Pause(nowTime);
26     }
27     Resume(nowTime);
28 }
29 
Stop()30 PandaRuntimeTimer *PandaRuntimeTimer::Stop()
31 {
32     uint64_t nowTime = Now();
33     Pause(nowTime);
34     if (parent_ != nullptr) {
35         parent_->Resume(nowTime);
36     }
37     UpdateCallerState();
38     elapsed_ = 0;
39     return parent_;
40 }
41 
Pause(uint64_t now)42 void PandaRuntimeTimer::Pause(uint64_t now)
43 {
44     if (!IsStarted()) {
45         return;
46     }
47     elapsed_ += (now - start_);
48     start_ = 0;
49 }
50 
Resume(uint64_t now)51 void PandaRuntimeTimer::Resume(uint64_t now)
52 {
53     if (IsStarted()) {
54         return;
55     }
56     start_ = now;
57 }
58 
Snapshot()59 void PandaRuntimeTimer::Snapshot()
60 {
61     uint64_t nowTime = Now();
62     Pause(nowTime);
63 
64     PandaRuntimeTimer *timer = this;
65     while (timer != nullptr) {
66         timer->UpdateCallerState();
67         timer = timer->parent_;
68     }
69     Resume(nowTime);
70 }
71 }  // namespace panda::ecmascript