-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathappletrace.mm
273 lines (234 loc) · 6.09 KB
/
appletrace.mm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
//
// appletrace.m
// appletrace
//
// Created by everettjf on 2017/9/12.
// Copyright © 2017年 everettjf. All rights reserved.
//
#import "appletrace.h"
#include <fcntl.h>
#include <mach/mach_time.h>
#include <map>
#include <pthread.h>
#include <string>
#include <sys/mman.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <unistd.h>
#include <vector>
// When appletracedata directory exists, whether delete or use another directory name (by adding sequence number)
// 0 for delete
// 1 for use another name
#define kAppleTraceData_IncreaseSeqWhenExist 0
#define IGNORE_FUNC_TRACE __attribute__((__annotate__(("ignore_appletrace"))))
namespace appletrace {
class Logger {
private:
// int block_size = 64 * 1024 * 1024; // 64MB
int block_size = 16 * 1024 * 1024; // 16MB
// int block_size = 1 * 1024 * 1024; // 1MB
// int block_size = 1 * 1024; // 1KB
int fd_ = 0;
char *file_start_ = NULL;
char *file_cur_ = NULL;
size_t cur_size_ = 0;
public:
Logger() {}
~Logger() {}
bool Open(const char *log_path) {
Close();
remove(log_path);
fd_ = ::open(log_path, O_CREAT | O_RDWR, (mode_t)0600);
if (fd_ == -1) {
NSLog(@"open file failed");
return false;
}
::lseek(fd_, block_size - 1, SEEK_SET);
ssize_t wrote_bytes = ::write(fd_, "", 1);
if (wrote_bytes != 1) {
NSLog(@"wrote error");
Close();
return false;
}
::lseek(fd_, 0, SEEK_SET);
file_start_ = (char *)::mmap(NULL, block_size, PROT_READ | PROT_WRITE, MAP_SHARED, fd_, 0);
if (file_start_ == MAP_FAILED) {
NSLog(@"map failed");
Close();
return false;
}
file_cur_ = file_start_;
return true;
}
void Close() {
if (file_start_) {
::munmap(file_start_, block_size);
}
if (fd_) {
::close(fd_);
}
fd_ = 0;
file_start_ = NULL;
file_cur_ = NULL;
cur_size_ = 0;
}
bool AddLine(const char *line) {
if (!file_cur_)
return false;
size_t len = strlen(line);
if (cur_size_ + len + 1 > block_size) {
NSLog(@"file full");
return false;
}
memcpy(file_cur_, line, len);
file_cur_ += len;
memcpy(file_cur_, (const char *)"\n", 1);
file_cur_ += 1;
cur_size_ += len + 1;
return true;
}
};
class LoggerManager {
private:
static int file_counter;
Logger log_;
static NSString *work_dir;
public:
std::string GetFilePath() {
if (!work_dir) {
NSString *rootdir = NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSUserDomainMask, YES)[0];
work_dir = [rootdir stringByAppendingPathComponent:@"appletracedata"];
NSFileManager *fm = [NSFileManager defaultManager];
#if kAppleTraceData_IncreaseSeqWhenExist
int seq = 1;
while ([fm fileExistsAtPath:work_dir]) {
NSString *dirname = [NSString stringWithFormat:@"appletracedata_%d", seq];
work_dir = [rootdir stringByAppendingPathComponent:dirname];
++seq;
}
#else
[fm removeItemAtPath:work_dir error:nil];
#endif
[fm createDirectoryAtPath:work_dir withIntermediateDirectories:YES attributes:nil error:nil];
}
NSString *log_name;
if (file_counter == 0) {
log_name = @"trace.appletrace";
} else {
log_name = [NSString stringWithFormat:@"trace_%@.appletrace", @(file_counter)];
}
NSString *log_path = [work_dir stringByAppendingPathComponent:log_name];
NSLog(@"log path = %@", log_path);
return std::string(log_path.UTF8String);
}
bool Open() {
std::string path = GetFilePath();
if (!log_.Open(path.c_str())) {
return false;
}
++file_counter;
return true;
}
void AddLine(const char *line) {
if (log_.AddLine(line))
return;
NSLog(@"will map a new file");
// map a new file
if (!Open())
return;
if (!log_.AddLine(line)) {
// error
NSLog(@"still error add line");
}
}
};
int LoggerManager::file_counter = 0;
NSString *LoggerManager::work_dir = nil;
class Trace {
private:
LoggerManager log_;
dispatch_queue_t queue_;
uint64_t begin_;
mach_timebase_info_data_t timeinfo_;
__uint64_t main_thread_id = 0;
public:
bool Open() {
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
log_.Open();
queue_ = dispatch_queue_create("appletrace.queue", DISPATCH_QUEUE_SERIAL);
mach_timebase_info(&timeinfo_);
begin_ = mach_absolute_time() * timeinfo_.numer / timeinfo_.denom;
});
return true;
}
void WriteSection(const char *name, const char *ph) {
pthread_t thread = pthread_self();
__uint64_t thread_id = 0;
pthread_threadid_np(thread, &thread_id);
uint64_t time = mach_absolute_time() * timeinfo_.numer / timeinfo_.denom;
uint64_t elapsed = (time - begin_) / 1000.0;
if (main_thread_id == 0 && pthread_main_np() != 0) {
main_thread_id = thread_id;
}
if (main_thread_id == thread_id) {
thread_id = 0; // just make main thread id zero
}
NSString *str = [NSString stringWithFormat:@"{\"name\":\"%s\",\"cat\":\"catname\",\"ph\":\"%s\",\"pid\":666,\"tid\":%llu,\"ts\":%llu}",
name, ph, thread_id, elapsed];
dispatch_async(queue_, ^{
log_.AddLine(str.UTF8String);
});
}
void SyncWait() {
dispatch_sync(queue_, ^{
NSLog(@"AppleTrace SyncWait");
});
}
};
class TraceManager {
private:
Trace t_;
public:
static TraceManager &Instance() {
static TraceManager o;
return o;
}
TraceManager() {
if (!t_.Open()) {
printf("error open trace file\n");
}
}
void BeginSection(const char *name) {
t_.WriteSection(name, "B");
}
void EndSection(const char *name) {
t_.WriteSection(name, "E");
}
void SyncWait() {
t_.SyncWait();
}
};
} // namespace appletrace
void _kk_APTBeginSection(const char *name) IGNORE_FUNC_TRACE {
appletrace::TraceManager::Instance().BeginSection(name);
}
void _kk_APTEndSection(const char *name) IGNORE_FUNC_TRACE {
appletrace::TraceManager::Instance().EndSection(name);
}
void _kk_APTSyncWait() IGNORE_FUNC_TRACE {
appletrace::TraceManager::Instance().SyncWait();
}
@interface APTInterface : NSObject
@end
@implementation APTInterface
+ (void)syncWait IGNORE_FUNC_TRACE {
_kk_APTSyncWait();
}
+ (void)test {
[self syncWait];
}
- (void)test2 {
[APTInterface test];
}
@end