ChampSim
deadlock.h
Go to the documentation of this file.
1 /*
2  * Copyright 2023 The ChampSim Contributors
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 #ifndef DEADLOCK_H
18 #define DEADLOCK_H
19 
20 #include <optional>
21 #include <string>
22 #include <type_traits>
23 #include <fmt/core.h>
24 #include <fmt/ranges.h>
25 
26 #include "util/type_traits.h"
27 
28 namespace champsim
29 {
30 namespace detail
31 {
32 template <typename>
34 };
35 
36 template <typename... Args>
37 struct fmtstr_type_finder<std::tuple<Args...>> {
38  using type = fmt::format_string<Args...>;
39 };
40 
41 template <typename R, typename F>
43 } // namespace detail
44 
45 // LCOV_EXCL_START Exclude the following function from LCOV
46 template <typename R, typename F>
47 void range_print_deadlock(const R& range, std::string kind_name, detail::fmtstr_type<R, F> fmtstr, F&& packing_func)
48 {
49  if (std::empty(range)) {
50  fmt::print("{} empty\n\n", kind_name);
51  return;
52  }
53 
54  auto unpacker = [fmtstr](auto... args) -> std::string {
55  return fmt::format(fmtstr, args...);
56  };
57 
58  auto formatter = [unpacker, &packing_func](auto entry) -> std::string {
59  if constexpr (champsim::is_specialization_v<std::decay_t<decltype(entry)>, std::optional>) {
60  if (!entry.has_value()) {
61  return std::string{"empty"};
62  }
63  }
64  return std::apply(unpacker, packing_func(entry));
65  };
66 
67  std::size_t j = 0;
68  for (auto entry : range) {
69  fmt::print("[{:s}] entry: {:>3} {:s}\n", kind_name, j++, formatter(entry));
70  }
71  fmt::print("\n");
72 }
73 // LCOV_EXCL_STOP
74 } // namespace champsim
75 
76 #endif
typename fmtstr_type_finder< std::invoke_result_t< F, typename R::value_type > >::type fmtstr_type
Definition: deadlock.h:42
Definition: champsim.h:24
void range_print_deadlock(const R &range, std::string kind_name, detail::fmtstr_type< R, F > fmtstr, F &&packing_func)
Definition: deadlock.h:47
constexpr bool is_specialization_v
Definition: type_traits.h:25
fmt::format_string< Args... > type
Definition: deadlock.h:38
Definition: deadlock.h:33