OSDN Git Service

Merge "Revert "Crypto performance benchmark"" into nyc-dev
[android-x86/system-extras.git] / simpleperf / utils.cpp
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 #include "utils.h"
18
19 #include <dirent.h>
20 #include <errno.h>
21 #include <fcntl.h>
22 #include <stdarg.h>
23 #include <stdio.h>
24 #include <sys/stat.h>
25 #include <unistd.h>
26
27 #include <algorithm>
28 #include <string>
29
30 #include <android-base/file.h>
31 #include <android-base/logging.h>
32
33 void OneTimeFreeAllocator::Clear() {
34   for (auto& p : v_) {
35     delete[] p;
36   }
37   v_.clear();
38   cur_ = nullptr;
39   end_ = nullptr;
40 }
41
42 const char* OneTimeFreeAllocator::AllocateString(const std::string& s) {
43   size_t size = s.size() + 1;
44   if (cur_ + size > end_) {
45     size_t alloc_size = std::max(size, unit_size_);
46     char* p = new char[alloc_size];
47     v_.push_back(p);
48     cur_ = p;
49     end_ = p + alloc_size;
50   }
51   strcpy(cur_, s.c_str());
52   const char* result = cur_;
53   cur_ += size;
54   return result;
55 }
56
57
58 FileHelper FileHelper::OpenReadOnly(const std::string& filename) {
59     int fd = TEMP_FAILURE_RETRY(open(filename.c_str(), O_RDONLY | O_BINARY));
60     return FileHelper(fd);
61 }
62
63 FileHelper FileHelper::OpenWriteOnly(const std::string& filename) {
64     int fd = TEMP_FAILURE_RETRY(open(filename.c_str(), O_WRONLY | O_BINARY | O_CREAT, 0644));
65     return FileHelper(fd);
66 }
67
68 FileHelper::~FileHelper() {
69   if (fd_ != -1) {
70     close(fd_);
71   }
72 }
73
74 ArchiveHelper::ArchiveHelper(int fd, const std::string& debug_filename) : valid_(false) {
75   int rc = OpenArchiveFd(fd, "", &handle_, false);
76   if (rc == 0) {
77     valid_ = true;
78   } else {
79     LOG(ERROR) << "Failed to open archive " << debug_filename << ": " << ErrorCodeString(rc);
80   }
81 }
82
83 ArchiveHelper::~ArchiveHelper() {
84   if (valid_) {
85     CloseArchive(handle_);
86   }
87 }
88
89 void PrintIndented(size_t indent, const char* fmt, ...) {
90   va_list ap;
91   va_start(ap, fmt);
92   printf("%*s", static_cast<int>(indent * 2), "");
93   vprintf(fmt, ap);
94   va_end(ap);
95 }
96
97 bool IsPowerOfTwo(uint64_t value) {
98   return (value != 0 && ((value & (value - 1)) == 0));
99 }
100
101 void GetEntriesInDir(const std::string& dirpath, std::vector<std::string>* files,
102                      std::vector<std::string>* subdirs) {
103   if (files != nullptr) {
104     files->clear();
105   }
106   if (subdirs != nullptr) {
107     subdirs->clear();
108   }
109   DIR* dir = opendir(dirpath.c_str());
110   if (dir == nullptr) {
111     PLOG(DEBUG) << "can't open dir " << dirpath;
112     return;
113   }
114   dirent* entry;
115   while ((entry = readdir(dir)) != nullptr) {
116     if (strcmp(entry->d_name, ".") == 0 || strcmp(entry->d_name, "..") == 0) {
117       continue;
118     }
119     if (IsDir(dirpath + std::string("/") + entry->d_name)) {
120       if (subdirs != nullptr) {
121         subdirs->push_back(entry->d_name);
122       }
123     } else {
124       if (files != nullptr) {
125         files->push_back(entry->d_name);
126       }
127     }
128   }
129   closedir(dir);
130 }
131
132 bool IsDir(const std::string& dirpath) {
133   struct stat st;
134   if (stat(dirpath.c_str(), &st) == 0) {
135     if (S_ISDIR(st.st_mode)) {
136       return true;
137     }
138   }
139   return false;
140 }
141
142 bool IsRegularFile(const std::string& filename) {
143   struct stat st;
144   if (stat(filename.c_str(), &st) == 0) {
145     if (S_ISREG(st.st_mode)) {
146       return true;
147     }
148   }
149   return false;
150 }
151
152 uint64_t GetFileSize(const std::string& filename) {
153   struct stat st;
154   if (stat(filename.c_str(), &st) == 0) {
155     return static_cast<uint64_t>(st.st_size);
156   }
157   return 0;
158 }
159
160 bool MkdirWithParents(const std::string& path) {
161   size_t prev_end = 0;
162   while (prev_end < path.size()) {
163     size_t next_end = path.find('/', prev_end + 1);
164     if (next_end == std::string::npos) {
165       break;
166     }
167     std::string dir_path = path.substr(0, next_end);
168     if (!IsDir(dir_path)) {
169 #if defined(_WIN32)
170       int ret = mkdir(dir_path.c_str());
171 #else
172       int ret = mkdir(dir_path.c_str(), 0755);
173 #endif
174       if (ret != 0) {
175         PLOG(ERROR) << "failed to create dir " << dir_path;
176         return false;
177       }
178     }
179     prev_end = next_end;
180   }
181   return true;
182 }