utils.h revision cc2e59e2478d330c89eaceda0dcc1f05ee32fbf8
1/*
2 * Copyright (C) 2015 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#ifndef SIMPLE_PERF_UTILS_H_
18#define SIMPLE_PERF_UTILS_H_
19
20#include <signal.h>
21#include <stddef.h>
22#include <stdio.h>
23#include <stdlib.h>
24
25#include <string>
26#include <vector>
27
28#define ALIGN(value, alignment) (((value) + (alignment)-1) & ~((alignment)-1))
29
30class LineReader {
31 public:
32  LineReader(FILE* fp) : fp_(fp), buf_(nullptr), bufsize_(0) {
33  }
34
35  ~LineReader() {
36    free(buf_);
37    fclose(fp_);
38  }
39
40  char* ReadLine() {
41    if (getline(&buf_, &bufsize_, fp_) != -1) {
42      return buf_;
43    }
44    return nullptr;
45  }
46
47  size_t MaxLineSize() {
48    return bufsize_;
49  }
50
51 private:
52  FILE* fp_;
53  char* buf_;
54  size_t bufsize_;
55};
56
57class SignalHandlerRegister {
58 public:
59  SignalHandlerRegister(const std::vector<int>& signums, void (*handler)(int)) {
60    for (auto& sig : signums) {
61      sig_t old_handler = signal(sig, handler);
62      saved_signal_handlers_.push_back(std::make_pair(sig, old_handler));
63    }
64  }
65
66  ~SignalHandlerRegister() {
67    for (auto& pair : saved_signal_handlers_) {
68      signal(pair.first, pair.second);
69    }
70  }
71
72 private:
73  std::vector<std::pair<int, sig_t>> saved_signal_handlers_;
74};
75
76// OneTimeAllocator is used to allocate memory many times and free only once at the end.
77// It reduces the cost to free each allocated memory.
78class OneTimeFreeAllocator {
79 public:
80  OneTimeFreeAllocator(size_t unit_size = 8192u)
81      : unit_size_(unit_size), cur_(nullptr), end_(nullptr) {
82  }
83
84  ~OneTimeFreeAllocator() {
85    Clear();
86  }
87
88  void Clear();
89  const char* AllocateString(const std::string& s);
90
91 private:
92  const size_t unit_size_;
93  std::vector<char*> v_;
94  char* cur_;
95  char* end_;
96};
97
98template <class T>
99void MoveFromBinaryFormat(T& data, const char*& p) {
100  data = *reinterpret_cast<const T*>(p);
101  p += sizeof(T);
102}
103
104void PrintIndented(size_t indent, const char* fmt, ...);
105
106bool IsPowerOfTwo(uint64_t value);
107
108void GetEntriesInDir(const std::string& dirpath, std::vector<std::string>* files,
109                     std::vector<std::string>* subdirs);
110bool IsDir(const std::string& dirpath);
111bool RemovePossibleFile(const std::string& filename);
112bool StringToPid(const std::string& s, int* pid);
113
114#endif  // SIMPLE_PERF_UTILS_H_
115