1 // Copyright (c) 2012 The Chromium OS Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 
5 #ifndef CHROMIUMOS_WIDE_PROFILING_UTILS_H_
6 #define CHROMIUMOS_WIDE_PROFILING_UTILS_H_
7 
8 #include <stdint.h>
9 #include <stdlib.h>  // for free()
10 
11 #include <memory>
12 #include <string>
13 #include <vector>
14 
15 #include "base/logging.h"
16 
17 #include "perf_internals.h"
18 #include "quipper_string.h"
19 
20 namespace quipper {
21 
22 struct FreeDeleter {
operatorFreeDeleter23   inline void operator()(void* pointer) {
24     free(pointer);
25   }
26 };
27 
28 template <typename T>
29 using malloced_unique_ptr = std::unique_ptr<T, FreeDeleter>;
30 
31 // Given a valid open file handle |fp|, returns the size of the file.
32 int64_t GetFileSizeFromHandle(FILE* fp);
33 
34 event_t* CallocMemoryForEvent(size_t size);
35 event_t* ReallocMemoryForEvent(event_t* event, size_t new_size);
36 
37 build_id_event* CallocMemoryForBuildID(size_t size);
38 
39 bool FileToBuffer(const string& filename, std::vector<char>* contents);
40 
41 template <typename CharContainer>
BufferToFile(const string & filename,const CharContainer & contents)42 bool BufferToFile(const string& filename, const CharContainer& contents) {
43   FILE* fp = fopen(filename.c_str(), "wb");
44   if (!fp)
45     return false;
46   // Do not write anything if |contents| contains nothing.  fopen will create
47   // an empty file.
48   if (!contents.empty()) {
49     CHECK_EQ(fwrite(contents.data(),
50                     sizeof(typename CharContainer::value_type),
51                     contents.size(),
52                     fp),
53              contents.size());
54   }
55   fclose(fp);
56   return true;
57 }
58 
59 uint64_t Md5Prefix(const string& input);
60 uint64_t Md5Prefix(const std::vector<char>& input);
61 
62 // Returns a string that represents |array| in hexadecimal.
63 string HexToString(const u8* array, size_t length);
64 
65 // Converts |str| to a hexadecimal number, stored in |array|.  Returns true on
66 // success.  Only stores up to |length| bytes - if there are more characters in
67 // the string, they are ignored (but the function may still return true).
68 bool StringToHex(const string& str, u8* array, size_t length);
69 
70 // Adjust |size| to blocks of |align_size|.  i.e. returns the smallest multiple
71 // of |align_size| that can fit |size|.
72 uint64_t AlignSize(uint64_t size, uint32_t align_size);
73 
74 // Given a general perf sample format |sample_type|, return the fields of that
75 // format that are present in a sample for an event of type |event_type|.
76 //
77 // e.g. FORK and EXIT events have the fields {time, pid/tid, cpu, id}.
78 // Given a sample type with fields {ip, time, pid/tid, and period}, return
79 // the intersection of these two field sets: {time, pid/tid}.
80 //
81 // All field formats are bitfields, as defined by enum perf_event_sample_format
82 // in kernel/perf_event.h.
83 uint64_t GetSampleFieldsForEventType(uint32_t event_type, uint64_t sample_type);
84 
85 // Returns the offset in bytes within a perf event structure at which the raw
86 // perf sample data is located.
87 uint64_t GetPerfSampleDataOffset(const event_t& event);
88 
89 // Returns the size of the 8-byte-aligned memory for storing |string|.
90 size_t GetUint64AlignedStringLength(const string& str);
91 
92 // Returns true iff the file exists.
93 bool FileExists(const string& filename);
94 
95 // Reads the contents of a file into |data|.  Returns true on success, false if
96 // it fails.
97 bool ReadFileToData(const string& filename, std::vector<char>* data);
98 
99 // Writes contents of |data| to a file with name |filename|, overwriting any
100 // existing file.  Returns true on success, false if it fails.
101 bool WriteDataToFile(const std::vector<char>& data, const string& filename);
102 
103 // Executes |command| and stores stdout output in |output|.  Returns true on
104 // success, false otherwise.
105 bool RunCommandAndGetStdout(const string& command, std::vector<char>* output);
106 
107 // Trim leading and trailing whitespace from |str|.
108 void TrimWhitespace(string* str);
109 
110 }  // namespace quipper
111 
112 #endif  // CHROMIUMOS_WIDE_PROFILING_UTILS_H_
113