Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(335)

Side by Side Diff: third_party/tcmalloc/chromium/src/profile-handler.cc

Issue 1076002: Revert 41938 - Merged third_party/tcmalloc/vendor/src(googleperftools r87) in... (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src/
Patch Set: Created 10 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
1 // Copyright (c) 2009, Google Inc. 1 // Copyright (c) 2009, Google Inc.
2 // All rights reserved. 2 // All rights reserved.
3 // 3 //
4 // Redistribution and use in source and binary forms, with or without 4 // Redistribution and use in source and binary forms, with or without
5 // modification, are permitted provided that the following conditions are 5 // modification, are permitted provided that the following conditions are
6 // met: 6 // met:
7 // 7 //
8 // * Redistributions of source code must retain the above copyright 8 // * Redistributions of source code must retain the above copyright
9 // notice, this list of conditions and the following disclaimer. 9 // notice, this list of conditions and the following disclaimer.
10 // * Redistributions in binary form must reproduce the above 10 // * Redistributions in binary form must reproduce the above
(...skipping 71 matching lines...) Expand 10 before | Expand all | Expand 10 after
82 // 82 //
83 // Prior to determining whether timers are shared, this function will 83 // Prior to determining whether timers are shared, this function will
84 // unconditionally start the timer. However, if this function determines 84 // unconditionally start the timer. However, if this function determines
85 // that timers are shared, then it will stop the timer if no callbacks are 85 // that timers are shared, then it will stop the timer if no callbacks are
86 // currently registered. 86 // currently registered.
87 void RegisterThread(); 87 void RegisterThread();
88 88
89 // Registers a callback routine to receive profile timer ticks. The returned 89 // Registers a callback routine to receive profile timer ticks. The returned
90 // token is to be used when unregistering this callback and must not be 90 // token is to be used when unregistering this callback and must not be
91 // deleted by the caller. Registration of the first callback enables the 91 // deleted by the caller. Registration of the first callback enables the
92 // SIGPROF handler (or SIGALRM if using ITIMER_REAL). 92 // SIGPROF handler.
93 ProfileHandlerToken* RegisterCallback(ProfileHandlerCallback callback, 93 ProfileHandlerToken* RegisterCallback(ProfileHandlerCallback callback,
94 void* callback_arg); 94 void* callback_arg);
95 95
96 // Unregisters a previously registered callback. Expects the token returned 96 // Unregisters a previously registered callback. Expects the token returned
97 // by the corresponding RegisterCallback routine. Unregistering the last 97 // by the corresponding RegisterCallback routine. Unregistering the last
98 // callback disables the SIGPROF handler (or SIGALRM if using ITIMER_REAL). 98 // callback disables the SIGPROF handler.
99 void UnregisterCallback(ProfileHandlerToken* token) 99 void UnregisterCallback(ProfileHandlerToken* token)
100 NO_THREAD_SAFETY_ANALYSIS; 100 NO_THREAD_SAFETY_ANALYSIS;
101 101
102 // Unregisters all the callbacks, stops the timer if shared, disables the 102 // Unregisters all the callbacks, stops the timer if shared, disables the
103 // SIGPROF (or SIGALRM) handler and clears the timer_sharing_ state. 103 // SIGPROF handler and clears the timer_sharing_ state.
104 void Reset(); 104 void Reset();
105 105
106 // Gets the current state of profile handler. 106 // Gets the current state of profile handler.
107 void GetState(ProfileHandlerState* state); 107 void GetState(ProfileHandlerState* state);
108 108
109 // Initializes and returns the ProfileHandler singleton. 109 // Initializes and returns the ProfileHandler singleton.
110 static ProfileHandler* Instance(); 110 static ProfileHandler* Instance();
111 111
112 private: 112 private:
113 ProfileHandler(); 113 ProfileHandler();
114 ~ProfileHandler(); 114 ~ProfileHandler();
115 115
116 // Largest allowed frequency. 116 // Largest allowed frequency.
117 static const int32 kMaxFrequency = 4000; 117 static const int32 kMaxFrequency = 4000;
118 // Default frequency. 118 // Default frequency.
119 static const int32 kDefaultFrequency = 100; 119 static const int32 kDefaultFrequency = 100;
120 120
121 // ProfileHandler singleton. 121 // ProfileHandler singleton.
122 static ProfileHandler* instance_; 122 static ProfileHandler* instance_;
123 123
124 // pthread_once_t for one time initialization of ProfileHandler singleton. 124 // pthread_once_t for one time initialization of ProfileHandler singleton.
125 static pthread_once_t once_; 125 static pthread_once_t once_;
126 126
127 // Initializes the ProfileHandler singleton via GoogleOnceInit. 127 // Initializes the ProfileHandler singleton via GoogleOnceInit.
128 static void Init(); 128 static void Init();
129 129
130 // The number of SIGPROF (or SIGALRM for ITIMER_REAL) interrupts received. 130 // Counts the number of SIGPROF interrupts received.
131 int64 interrupts_ GUARDED_BY(signal_lock_); 131 int64 interrupts_ GUARDED_BY(signal_lock_);
132 132
133 // SIGPROF/SIGALRM interrupt frequency, read-only after construction. 133 // SIGPROF interrupt frequency, read-only after construction.
134 int32 frequency_; 134 int32 frequency_;
135 135
136 // ITIMER_PROF (which uses SIGPROF), or ITIMER_REAL (which uses SIGALRM)
137 int timer_type_;
138
139 // Counts the number of callbacks registered. 136 // Counts the number of callbacks registered.
140 int32 callback_count_ GUARDED_BY(control_lock_); 137 int32 callback_count_ GUARDED_BY(control_lock_);
141 138
142 // Whether or not the threading system provides interval timers that are 139 // Whether or not the threading system provides interval timers that are
143 // shared by all threads in a process. 140 // shared by all threads in a process.
144 enum { 141 enum {
145 // No timer initialization attempted yet. 142 // No timer initialization attempted yet.
146 TIMERS_UNTOUCHED, 143 TIMERS_UNTOUCHED,
147 // First thread has registered and set timer. 144 // First thread has registered and set timer.
148 TIMERS_ONE_SET, 145 TIMERS_ONE_SET,
(...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after
192 // thread. This actually checks the kernel's interval timer setting. (It is 189 // thread. This actually checks the kernel's interval timer setting. (It is
193 // used to detect whether timers are shared or separate.) 190 // used to detect whether timers are shared or separate.)
194 bool IsTimerRunning() EXCLUSIVE_LOCKS_REQUIRED(control_lock_); 191 bool IsTimerRunning() EXCLUSIVE_LOCKS_REQUIRED(control_lock_);
195 192
196 // Sets the timer interrupt signal handler. 193 // Sets the timer interrupt signal handler.
197 void EnableHandler() EXCLUSIVE_LOCKS_REQUIRED(control_lock_); 194 void EnableHandler() EXCLUSIVE_LOCKS_REQUIRED(control_lock_);
198 195
199 // Disables (ignores) the timer interrupt signal. 196 // Disables (ignores) the timer interrupt signal.
200 void DisableHandler() EXCLUSIVE_LOCKS_REQUIRED(control_lock_); 197 void DisableHandler() EXCLUSIVE_LOCKS_REQUIRED(control_lock_);
201 198
202 // SIGPROF/SIGALRM handler. Iterate over and call all the registered callbacks . 199 // SIGPROF handler. Iterate over and call all the registered callbacks.
203 static void SignalHandler(int sig, siginfo_t* sinfo, void* ucontext); 200 static void SignalHandler(int sig, siginfo_t* sinfo, void* ucontext);
204 201
205 DISALLOW_COPY_AND_ASSIGN(ProfileHandler); 202 DISALLOW_EVIL_CONSTRUCTORS(ProfileHandler);
206 }; 203 };
207 204
208 ProfileHandler* ProfileHandler::instance_ = NULL; 205 ProfileHandler* ProfileHandler::instance_ = NULL;
209 pthread_once_t ProfileHandler::once_ = PTHREAD_ONCE_INIT; 206 pthread_once_t ProfileHandler::once_ = PTHREAD_ONCE_INIT;
210 207
211 const int32 ProfileHandler::kMaxFrequency; 208 const int32 ProfileHandler::kMaxFrequency;
212 const int32 ProfileHandler::kDefaultFrequency; 209 const int32 ProfileHandler::kDefaultFrequency;
213 210
214 // If we are LD_PRELOAD-ed against a non-pthreads app, then 211 // If we are LD_PRELOAD-ed against a non-pthreads app, then
215 // pthread_once won't be defined. We declare it here, for that 212 // pthread_once won't be defined. We declare it here, for that
(...skipping 21 matching lines...) Expand all
237 assert(instance_ != NULL); 234 assert(instance_ != NULL);
238 } 235 }
239 return instance_; 236 return instance_;
240 } 237 }
241 238
242 ProfileHandler::ProfileHandler() 239 ProfileHandler::ProfileHandler()
243 : interrupts_(0), 240 : interrupts_(0),
244 callback_count_(0), 241 callback_count_(0),
245 timer_sharing_(TIMERS_UNTOUCHED) { 242 timer_sharing_(TIMERS_UNTOUCHED) {
246 SpinLockHolder cl(&control_lock_); 243 SpinLockHolder cl(&control_lock_);
247
248 timer_type_ = (getenv("CPUPROFILE_REALTIME") ? ITIMER_REAL : ITIMER_PROF);
249
250 // Get frequency of interrupts (if specified) 244 // Get frequency of interrupts (if specified)
251 char junk; 245 char junk;
252 const char* fr = getenv("CPUPROFILE_FREQUENCY"); 246 const char* fr = getenv("CPUPROFILE_FREQUENCY");
253 if (fr != NULL && (sscanf(fr, "%u%c", &frequency_, &junk) == 1) && 247 if (fr != NULL && (sscanf(fr, "%u%c", &frequency_, &junk) == 1) &&
254 (frequency_ > 0)) { 248 (frequency_ > 0)) {
255 // Limit to kMaxFrequency 249 // Limit to kMaxFrequency
256 frequency_ = (frequency_ > kMaxFrequency) ? kMaxFrequency : frequency_; 250 frequency_ = (frequency_ > kMaxFrequency) ? kMaxFrequency : frequency_;
257 } else { 251 } else {
258 frequency_ = kDefaultFrequency; 252 frequency_ = kDefaultFrequency;
259 } 253 }
(...skipping 129 matching lines...) Expand 10 before | Expand all | Expand 10 after
389 } 383 }
390 state->frequency = frequency_; 384 state->frequency = frequency_;
391 state->callback_count = callback_count_; 385 state->callback_count = callback_count_;
392 } 386 }
393 387
394 void ProfileHandler::StartTimer() { 388 void ProfileHandler::StartTimer() {
395 struct itimerval timer; 389 struct itimerval timer;
396 timer.it_interval.tv_sec = 0; 390 timer.it_interval.tv_sec = 0;
397 timer.it_interval.tv_usec = 1000000 / frequency_; 391 timer.it_interval.tv_usec = 1000000 / frequency_;
398 timer.it_value = timer.it_interval; 392 timer.it_value = timer.it_interval;
399 setitimer(timer_type_, &timer, 0); 393 setitimer(ITIMER_PROF, &timer, 0);
400 } 394 }
401 395
402 void ProfileHandler::StopTimer() { 396 void ProfileHandler::StopTimer() {
403 struct itimerval timer; 397 struct itimerval timer;
404 memset(&timer, 0, sizeof timer); 398 memset(&timer, 0, sizeof timer);
405 setitimer(timer_type_, &timer, 0); 399 setitimer(ITIMER_PROF, &timer, 0);
406 } 400 }
407 401
408 bool ProfileHandler::IsTimerRunning() { 402 bool ProfileHandler::IsTimerRunning() {
409 struct itimerval current_timer; 403 struct itimerval current_timer;
410 RAW_CHECK(0 == getitimer(timer_type_, &current_timer), "getitimer"); 404 RAW_CHECK(0 == getitimer(ITIMER_PROF, &current_timer), "getitimer");
411 return (current_timer.it_value.tv_sec != 0 || 405 return (current_timer.it_value.tv_sec != 0 ||
412 current_timer.it_value.tv_usec != 0); 406 current_timer.it_value.tv_usec != 0);
413 } 407 }
414 408
415 void ProfileHandler::EnableHandler() { 409 void ProfileHandler::EnableHandler() {
416 struct sigaction sa; 410 struct sigaction sa;
417 sa.sa_sigaction = SignalHandler; 411 sa.sa_sigaction = SignalHandler;
418 sa.sa_flags = SA_RESTART | SA_SIGINFO; 412 sa.sa_flags = SA_RESTART | SA_SIGINFO;
419 sigemptyset(&sa.sa_mask); 413 sigemptyset(&sa.sa_mask);
420 const int signal_number = (timer_type_ == ITIMER_PROF ? SIGPROF : SIGALRM); 414 RAW_CHECK(sigaction(SIGPROF, &sa, NULL) == 0, "sigprof (enable)");
421 RAW_CHECK(sigaction(signal_number, &sa, NULL) == 0, "sigprof (enable)");
422 } 415 }
423 416
424 void ProfileHandler::DisableHandler() { 417 void ProfileHandler::DisableHandler() {
425 struct sigaction sa; 418 struct sigaction sa;
426 sa.sa_handler = SIG_IGN; 419 sa.sa_handler = SIG_IGN;
427 sa.sa_flags = SA_RESTART; 420 sa.sa_flags = SA_RESTART;
428 sigemptyset(&sa.sa_mask); 421 sigemptyset(&sa.sa_mask);
429 const int signal_number = (timer_type_ == ITIMER_PROF ? SIGPROF : SIGALRM); 422 RAW_CHECK(sigaction(SIGPROF, &sa, NULL) == 0, "sigprof (disable)");
430 RAW_CHECK(sigaction(signal_number, &sa, NULL) == 0, "sigprof (disable)");
431 } 423 }
432 424
433 void ProfileHandler::SignalHandler(int sig, siginfo_t* sinfo, void* ucontext) { 425 void ProfileHandler::SignalHandler(int sig, siginfo_t* sinfo, void* ucontext) {
434 int saved_errno = errno; 426 int saved_errno = errno;
435 RAW_CHECK(instance_ != NULL, "ProfileHandler is not initialized"); 427 RAW_CHECK(instance_ != NULL, "ProfileHandler is not initialized");
436 { 428 {
437 SpinLockHolder sl(&instance_->signal_lock_); 429 SpinLockHolder sl(&instance_->signal_lock_);
438 ++instance_->interrupts_; 430 ++instance_->interrupts_;
439 for (CallbackIterator it = instance_->callbacks_.begin(); 431 for (CallbackIterator it = instance_->callbacks_.begin();
440 it != instance_->callbacks_.end(); 432 it != instance_->callbacks_.end();
441 ++it) { 433 ++it) {
442 (*it)->callback(sig, sinfo, ucontext, (*it)->callback_arg); 434 (*it)->callback(sig, sinfo, ucontext, (*it)->callback_arg);
443 } 435 }
444 } 436 }
445 errno = saved_errno; 437 errno = saved_errno;
446 } 438 }
447 439
448 // The sole purpose of this class is to initialize the ProfileHandler singleton 440 // The sole purpose of this class is to initialize the ProfileHandler singleton
449 // when the global static objects are created. Note that the main thread will 441 // when the global static objects are created. Note that the main thread will
450 // be registered at this time. 442 // be registered at this time.
451 class ProfileHandlerInitializer { 443 class ProfileHandlerInitializer {
452 public: 444 public:
453 ProfileHandlerInitializer() { 445 ProfileHandlerInitializer() {
454 ProfileHandler::Instance()->RegisterThread(); 446 ProfileHandler::Instance()->RegisterThread();
455 } 447 }
456 448
457 private: 449 private:
458 DISALLOW_COPY_AND_ASSIGN(ProfileHandlerInitializer); 450 DISALLOW_EVIL_CONSTRUCTORS(ProfileHandlerInitializer);
459 }; 451 };
460 // ProfileHandlerInitializer singleton 452 // ProfileHandlerInitializer singleton
461 static ProfileHandlerInitializer profile_handler_initializer; 453 static ProfileHandlerInitializer profile_handler_initializer;
462 454
463 extern "C" void ProfileHandlerRegisterThread() { 455 extern "C" void ProfileHandlerRegisterThread() {
464 ProfileHandler::Instance()->RegisterThread(); 456 ProfileHandler::Instance()->RegisterThread();
465 } 457 }
466 458
467 extern "C" ProfileHandlerToken* ProfileHandlerRegisterCallback( 459 extern "C" ProfileHandlerToken* ProfileHandlerRegisterCallback(
468 ProfileHandlerCallback callback, void* callback_arg) { 460 ProfileHandlerCallback callback, void* callback_arg) {
(...skipping 29 matching lines...) Expand all
498 extern "C" void ProfileHandlerUnregisterCallback(ProfileHandlerToken* token) { 490 extern "C" void ProfileHandlerUnregisterCallback(ProfileHandlerToken* token) {
499 } 491 }
500 492
501 extern "C" void ProfileHandlerReset() { 493 extern "C" void ProfileHandlerReset() {
502 } 494 }
503 495
504 extern "C" void ProfileHandlerGetState(ProfileHandlerState* state) { 496 extern "C" void ProfileHandlerGetState(ProfileHandlerState* state) {
505 } 497 }
506 498
507 #endif // OS_CYGWIN 499 #endif // OS_CYGWIN
OLDNEW
« no previous file with comments | « third_party/tcmalloc/chromium/src/pprof ('k') | third_party/tcmalloc/chromium/src/profiledata.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698