• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) Huawei Technologies Co., Ltd. 2020-2024. All rights reserved.
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 "util.h"
16 #include <exception>
17 #include <cassert>
18 
19 using namespace std;
20 
21 static int gThrowExceptionCnt = 0;
22 static int gCatchExceptionCnt = 0;
23 
24 struct TestException : public exception {
TestExceptionTestException25     TestException()
26     {
27         gThrowExceptionCnt++;
28     }
whatTestException29     const char* what() const _NOEXCEPT override
30     {
31         return "Test Exception!";
32     }
33 
check_resultTestException34     void check_result()
35     {
36         gCatchExceptionCnt++;
37     }
38 };
39 
40 // increase call stack depth
ThrowExceptionCallStub()41 void __attribute__((noinline)) ThrowExceptionCallStub()
42 {
43     throw TestException();
44 }
45 
46 // increase call stack depth
TopCallStub()47 void __attribute__((noinline)) TopCallStub()
48 {
49     ThrowExceptionCallStub();
50 }
51 
Bm_function_throw_exception(benchmark::State & state)52 static void Bm_function_throw_exception(benchmark::State &state)
53 {
54     int ret;
55     for (auto _ : state) {
56         try {
57             TopCallStub();
58         } catch (TestException& e) {
59             e.check_result();
60             ret = gThrowExceptionCnt;
61         }
62         benchmark::DoNotOptimize(ret);
63     }
64 }
65 
66 MUSL_BENCHMARK(Bm_function_throw_exception);
67