OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2006-2009 The Chromium 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 #include "webkit/extensions/v8/heap_profiler_extension.h" |
| 6 |
| 7 #include "base/basictypes.h" |
| 8 |
| 9 #if defined(LINUX_USE_TCMALLOC) |
| 10 #include "third_party/tcmalloc/tcmalloc/src/google/heap-profiler.h" |
| 11 #endif |
| 12 |
| 13 namespace extensions_v8 { |
| 14 |
| 15 namespace { |
| 16 |
| 17 const char kHeapProfilerExtensionName[] = "v8/HeapProfiler"; |
| 18 |
| 19 class HeapProfilerWrapper : public v8::Extension { |
| 20 public: |
| 21 HeapProfilerWrapper() |
| 22 : v8::Extension(kHeapProfilerExtensionName, |
| 23 "if (typeof(chromium) == 'undefined') {" |
| 24 " chromium = {};" |
| 25 "}" |
| 26 "(function() {" |
| 27 " native function HeapProfilerStart();" |
| 28 " native function HeapProfilerStop();" |
| 29 " native function HeapProfilerDump();" |
| 30 " chromium.HeapProfiler = {};" |
| 31 " chromium.HeapProfiler.start = function(opt_prefix) {" |
| 32 " var prefix = opt_prefix;" |
| 33 " if (!prefix) {" |
| 34 " var d = new Date();" |
| 35 " prefix = \"chromium-\" + " |
| 36 " (1900 + d.getYear()) + " |
| 37 " \"-\" + d.getMonth() + " |
| 38 " \"-\" + d.getDay() + " |
| 39 " \"-\" + d.getTime();" |
| 40 " }" |
| 41 " HeapProfilerStart(prefix);" |
| 42 " };" |
| 43 " chromium.HeapProfiler.stop = function() {" |
| 44 " HeapProfilerStop();" |
| 45 " };" |
| 46 " chromium.HeapProfiler.dump = function(opt_reason) {" |
| 47 " HeapProfilerDump(opt_reason || \"\");" |
| 48 " };" |
| 49 "})();") {} |
| 50 |
| 51 virtual v8::Handle<v8::FunctionTemplate> GetNativeFunction( |
| 52 v8::Handle<v8::String> name) { |
| 53 if (name->Equals(v8::String::New("HeapProfilerStart"))) { |
| 54 return v8::FunctionTemplate::New(HeapProfilerStart); |
| 55 } else if (name->Equals(v8::String::New("HeapProfilerStop"))) { |
| 56 return v8::FunctionTemplate::New(HeapProfilerStop); |
| 57 } else if (name->Equals(v8::String::New("HeapProfilerDump"))) { |
| 58 return v8::FunctionTemplate::New(HeapProfilerDump); |
| 59 } |
| 60 return v8::Handle<v8::FunctionTemplate>(); |
| 61 } |
| 62 |
| 63 #if defined(LINUX_USE_TCMALLOC) |
| 64 static v8::Handle<v8::Value> HeapProfilerStart(const v8::Arguments& args) { |
| 65 if (args.Length() >= 1 && args[0]->IsString()) { |
| 66 v8::Local<v8::String> js_prefix = args[0]->ToString(); |
| 67 char prefix[256]; |
| 68 js_prefix->WriteAscii(prefix, 0, arraysize(prefix) - 1); |
| 69 ::HeapProfilerStart(prefix); |
| 70 } |
| 71 return v8::Undefined(); |
| 72 } |
| 73 |
| 74 static v8::Handle<v8::Value> HeapProfilerStop(const v8::Arguments& args) { |
| 75 ::HeapProfilerStop(); |
| 76 return v8::Undefined(); |
| 77 } |
| 78 |
| 79 static v8::Handle<v8::Value> HeapProfilerDump(const v8::Arguments& args) { |
| 80 if (args.Length() >= 1 && args[0]->IsString()) { |
| 81 v8::Local<v8::String> js_reason = args[0]->ToString(); |
| 82 char reason[256]; |
| 83 js_reason->WriteAscii(reason, 0, arraysize(reason) - 1); |
| 84 ::HeapProfilerDump(reason); |
| 85 } |
| 86 return v8::Undefined(); |
| 87 } |
| 88 |
| 89 #else |
| 90 |
| 91 static v8::Handle<v8::Value> HeapProfilerStart(const v8::Arguments& args) { |
| 92 return v8::Undefined(); |
| 93 } |
| 94 |
| 95 static v8::Handle<v8::Value> HeapProfilerStop(const v8::Arguments& args) { |
| 96 return v8::Undefined(); |
| 97 } |
| 98 |
| 99 static v8::Handle<v8::Value> HeapProfilerDump(const v8::Arguments& args) { |
| 100 return v8::Undefined(); |
| 101 } |
| 102 #endif |
| 103 }; |
| 104 |
| 105 } // namespace |
| 106 |
| 107 v8::Extension* HeapProfilerExtension::Get() { |
| 108 return new HeapProfilerWrapper; |
| 109 } |
| 110 |
| 111 } // namespace extensions_v8 |
OLD | NEW |