Dean Michael Berris | e7dbebf | 2017-01-25 03:50:46 +0000 | [diff] [blame] | 1 | //===-- xray_utils.cc -------------------------------------------*- C++ -*-===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // This file is a part of XRay, a dynamic runtime instrumentation system. |
| 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | #include "xray_utils.h" |
| 14 | |
| 15 | #include "sanitizer_common/sanitizer_common.h" |
| 16 | #include "xray_defs.h" |
| 17 | #include "xray_flags.h" |
| 18 | #include <cstdio> |
Krzysztof Parzyszek | 520e51d | 2017-01-25 14:20:30 +0000 | [diff] [blame^] | 19 | #include <errno.h> |
Dean Michael Berris | e7dbebf | 2017-01-25 03:50:46 +0000 | [diff] [blame] | 20 | #include <fcntl.h> |
| 21 | #include <iterator> |
| 22 | #include <sys/types.h> |
| 23 | #include <tuple> |
| 24 | #include <unistd.h> |
| 25 | #include <utility> |
| 26 | |
| 27 | #if defined(__x86_64__) |
| 28 | #include "xray_x86_64.h" |
| 29 | #elif defined(__arm__) || defined(__aarch64__) |
| 30 | #include "xray_emulate_tsc.h" |
| 31 | #else |
| 32 | #error "Unsupported CPU Architecture" |
| 33 | #endif /* CPU architecture */ |
| 34 | |
| 35 | namespace __xray { |
| 36 | |
| 37 | void PrintToStdErr(const char *Buffer) XRAY_NEVER_INSTRUMENT { |
| 38 | fprintf(stderr, "%s", Buffer); |
| 39 | } |
| 40 | |
| 41 | void retryingWriteAll(int Fd, char *Begin, char *End) XRAY_NEVER_INSTRUMENT { |
| 42 | if (Begin == End) |
| 43 | return; |
| 44 | auto TotalBytes = std::distance(Begin, End); |
| 45 | while (auto Written = write(Fd, Begin, TotalBytes)) { |
| 46 | if (Written < 0) { |
| 47 | if (errno == EINTR) |
| 48 | continue; // Try again. |
| 49 | Report("Failed to write; errno = %d\n", errno); |
| 50 | return; |
| 51 | } |
| 52 | TotalBytes -= Written; |
| 53 | if (TotalBytes == 0) |
| 54 | break; |
| 55 | Begin += Written; |
| 56 | } |
| 57 | } |
| 58 | |
| 59 | std::pair<ssize_t, bool> retryingReadSome(int Fd, char *Begin, |
| 60 | char *End) XRAY_NEVER_INSTRUMENT { |
| 61 | auto BytesToRead = std::distance(Begin, End); |
| 62 | ssize_t BytesRead; |
| 63 | ssize_t TotalBytesRead = 0; |
| 64 | while (BytesToRead && (BytesRead = read(Fd, Begin, BytesToRead))) { |
| 65 | if (BytesRead == -1) { |
| 66 | if (errno == EINTR) |
| 67 | continue; |
| 68 | Report("Read error; errno = %d\n", errno); |
| 69 | return std::make_pair(TotalBytesRead, false); |
| 70 | } |
| 71 | |
| 72 | TotalBytesRead += BytesRead; |
| 73 | BytesToRead -= BytesRead; |
| 74 | Begin += BytesRead; |
| 75 | } |
| 76 | return std::make_pair(TotalBytesRead, true); |
| 77 | } |
| 78 | |
| 79 | bool readValueFromFile(const char *Filename, |
| 80 | long long *Value) XRAY_NEVER_INSTRUMENT { |
| 81 | int Fd = open(Filename, O_RDONLY | O_CLOEXEC); |
| 82 | if (Fd == -1) |
| 83 | return false; |
| 84 | static constexpr size_t BufSize = 256; |
| 85 | char Line[BufSize] = {}; |
| 86 | ssize_t BytesRead; |
| 87 | bool Success; |
| 88 | std::tie(BytesRead, Success) = retryingReadSome(Fd, Line, Line + BufSize); |
| 89 | if (!Success) |
| 90 | return false; |
| 91 | close(Fd); |
| 92 | char *End = nullptr; |
| 93 | long long Tmp = internal_simple_strtoll(Line, &End, 10); |
| 94 | bool Result = false; |
| 95 | if (Line[0] != '\0' && (*End == '\n' || *End == '\0')) { |
| 96 | *Value = Tmp; |
| 97 | Result = true; |
| 98 | } |
| 99 | return Result; |
| 100 | } |
| 101 | |
| 102 | long long getCPUFrequency() XRAY_NEVER_INSTRUMENT { |
| 103 | // Get the cycle frequency from SysFS on Linux. |
| 104 | long long CPUFrequency = -1; |
| 105 | #if defined(__x86_64__) |
| 106 | if (readValueFromFile("/sys/devices/system/cpu/cpu0/tsc_freq_khz", |
| 107 | &CPUFrequency)) { |
| 108 | CPUFrequency *= 1000; |
| 109 | } else if (readValueFromFile( |
| 110 | "/sys/devices/system/cpu/cpu0/cpufreq/cpuinfo_max_freq", |
| 111 | &CPUFrequency)) { |
| 112 | CPUFrequency *= 1000; |
| 113 | } else { |
| 114 | Report("Unable to determine CPU frequency for TSC accounting.\n"); |
| 115 | } |
| 116 | #elif defined(__arm__) || defined(__aarch64__) |
| 117 | // There is no instruction like RDTSCP in user mode on ARM. ARM's CP15 does |
| 118 | // not have a constant frequency like TSC on x86(_64), it may go faster |
| 119 | // or slower depending on CPU turbo or power saving mode. Furthermore, |
| 120 | // to read from CP15 on ARM a kernel modification or a driver is needed. |
| 121 | // We can not require this from users of compiler-rt. |
| 122 | // So on ARM we use clock_gettime() which gives the result in nanoseconds. |
| 123 | // To get the measurements per second, we scale this by the number of |
| 124 | // nanoseconds per second, pretending that the TSC frequency is 1GHz and |
| 125 | // one TSC tick is 1 nanosecond. |
| 126 | CPUFrequency = NanosecondsPerSecond; |
| 127 | #else |
| 128 | #error "Unsupported CPU Architecture" |
| 129 | #endif /* CPU architecture */ |
| 130 | return CPUFrequency; |
| 131 | } |
| 132 | |
| 133 | int getLogFD() XRAY_NEVER_INSTRUMENT { |
| 134 | // FIXME: Figure out how to make this less stderr-dependent. |
| 135 | SetPrintfAndReportCallback(PrintToStdErr); |
| 136 | // Open a temporary file once for the log. |
| 137 | static char TmpFilename[256] = {}; |
| 138 | static char TmpWildcardPattern[] = "XXXXXX"; |
| 139 | auto Argv = GetArgv(); |
| 140 | const char *Progname = Argv[0] == nullptr ? "(unknown)" : Argv[0]; |
| 141 | const char *LastSlash = internal_strrchr(Progname, '/'); |
| 142 | |
| 143 | if (LastSlash != nullptr) |
| 144 | Progname = LastSlash + 1; |
| 145 | |
| 146 | const int HalfLength = sizeof(TmpFilename) / 2 - sizeof(TmpWildcardPattern); |
| 147 | int NeededLength = internal_snprintf( |
| 148 | TmpFilename, sizeof(TmpFilename), "%.*s%.*s.%s", HalfLength, |
| 149 | flags()->xray_logfile_base, HalfLength, Progname, TmpWildcardPattern); |
| 150 | if (NeededLength > int(sizeof(TmpFilename))) { |
| 151 | Report("XRay log file name too long (%d): %s\n", NeededLength, TmpFilename); |
| 152 | return -1; |
| 153 | } |
| 154 | int Fd = mkstemp(TmpFilename); |
| 155 | if (Fd == -1) { |
| 156 | Report("XRay: Failed opening temporary file '%s'; not logging events.\n", |
| 157 | TmpFilename); |
| 158 | return -1; |
| 159 | } |
| 160 | if (Verbosity()) |
| 161 | fprintf(stderr, "XRay: Log file in '%s'\n", TmpFilename); |
| 162 | |
| 163 | return Fd; |
| 164 | } |
| 165 | |
| 166 | } // namespace __xray |