1 /*
2 * Copyright 2020 The Android Open Source Project
3 *
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
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
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.
15 */
16
17 #define LOG_TAG "BtStopWatch"
18
19 #include "common/stop_watch.h"
20
21 #include <bluetooth/log.h>
22
23 #include <array>
24 #include <iomanip>
25 #include <mutex>
26 #include <sstream>
27 #include <utility>
28
29 namespace bluetooth {
30 namespace common {
31
32 static const int LOG_BUFFER_LENGTH = 10;
33 static std::array<StopWatchLog, LOG_BUFFER_LENGTH> stopwatch_logs;
34 static int current_buffer_index;
35 static std::recursive_mutex stopwatch_log_mutex;
36
RecordLog(StopWatchLog log)37 void StopWatch::RecordLog(StopWatchLog log) {
38 std::unique_lock<std::recursive_mutex> lock(stopwatch_log_mutex, std::defer_lock);
39 if (!lock.try_lock()) {
40 log::info("try_lock fail. log content: {}, took {} us", log.message,
41 static_cast<size_t>(std::chrono::duration_cast<std::chrono::microseconds>(
42 stopwatch_logs[current_buffer_index].end_timestamp -
43 stopwatch_logs[current_buffer_index].start_timestamp)
44 .count()));
45 return;
46 }
47 if (current_buffer_index >= LOG_BUFFER_LENGTH) {
48 current_buffer_index = 0;
49 }
50 stopwatch_logs[current_buffer_index] = std::move(log);
51 current_buffer_index++;
52 lock.unlock();
53 }
54
DumpStopWatchLog()55 void StopWatch::DumpStopWatchLog() {
56 std::lock_guard<std::recursive_mutex> lock(stopwatch_log_mutex);
57 log::info("=-----------------------------------=");
58 log::info("bluetooth stopwatch log history:");
59 for (int i = 0; i < LOG_BUFFER_LENGTH; i++) {
60 if (current_buffer_index >= LOG_BUFFER_LENGTH) {
61 current_buffer_index = 0;
62 }
63 if (stopwatch_logs[current_buffer_index].message.empty()) {
64 current_buffer_index++;
65 continue;
66 }
67 std::stringstream ss;
68 auto now = stopwatch_logs[current_buffer_index].timestamp;
69 auto millis =
70 std::chrono::duration_cast<std::chrono::milliseconds>(now.time_since_epoch()) % 1000;
71 auto now_time_t = std::chrono::system_clock::to_time_t(now);
72 ss << std::put_time(std::localtime(&now_time_t), "%Y-%m-%d %H:%M:%S");
73 ss << '.' << std::setfill('0') << std::setw(3) << millis.count();
74 std::string start_timestamp = ss.str();
75 log::info("{}: {}: took {} us", start_timestamp, stopwatch_logs[current_buffer_index].message,
76 static_cast<size_t>(std::chrono::duration_cast<std::chrono::microseconds>(
77 stopwatch_logs[current_buffer_index].end_timestamp -
78 stopwatch_logs[current_buffer_index].start_timestamp)
79 .count()));
80 current_buffer_index++;
81 }
82 log::info("=-----------------------------------=");
83 }
84
StopWatch(std::string text)85 StopWatch::StopWatch(std::string text)
86 : text_(std::move(text)),
87 timestamp_(std::chrono::system_clock::now()),
88 start_timestamp_(std::chrono::high_resolution_clock::now()) {}
89
~StopWatch()90 StopWatch::~StopWatch() {
91 StopWatchLog sw_log;
92 sw_log.timestamp = timestamp_;
93 sw_log.start_timestamp = start_timestamp_;
94 sw_log.end_timestamp = std::chrono::high_resolution_clock::now();
95 sw_log.message = std::move(text_);
96
97 RecordLog(std::move(sw_log));
98 }
99
100 } // namespace common
101 } // namespace bluetooth
102