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

Side by Side Diff: src/d8.cc

Issue 13050: Live counters in d8 (Closed)
Patch Set: Created 12 years 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
« no previous file with comments | « src/d8.h ('k') | src/flag-definitions.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright 2008 the V8 project authors. All rights reserved. 1 // Copyright 2008 the V8 project authors. All rights reserved.
2 // Redistribution and use in source and binary forms, with or without 2 // Redistribution and use in source and binary forms, with or without
3 // modification, are permitted provided that the following conditions are 3 // modification, are permitted provided that the following conditions are
4 // met: 4 // met:
5 // 5 //
6 // * Redistributions of source code must retain the above copyright 6 // * Redistributions of source code must retain the above copyright
7 // notice, this list of conditions and the following disclaimer. 7 // notice, this list of conditions and the following disclaimer.
8 // * Redistributions in binary form must reproduce the above 8 // * Redistributions in binary form must reproduce the above
9 // copyright notice, this list of conditions and the following 9 // copyright notice, this list of conditions and the following
10 // disclaimer in the documentation and/or other materials provided 10 // disclaimer in the documentation and/or other materials provided
(...skipping 66 matching lines...) Expand 10 before | Expand all | Expand 10 after
77 i::SmartPointer<char> DumbLineEditor::Prompt(const char* prompt) { 77 i::SmartPointer<char> DumbLineEditor::Prompt(const char* prompt) {
78 static const int kBufferSize = 256; 78 static const int kBufferSize = 256;
79 char buffer[kBufferSize]; 79 char buffer[kBufferSize];
80 printf("%s", prompt); 80 printf("%s", prompt);
81 char* str = fgets(buffer, kBufferSize, stdin); 81 char* str = fgets(buffer, kBufferSize, stdin);
82 return i::SmartPointer<char>(str ? i::StrDup(str) : str); 82 return i::SmartPointer<char>(str ? i::StrDup(str) : str);
83 } 83 }
84 84
85 85
86 Shell::CounterMap Shell::counter_map_; 86 Shell::CounterMap Shell::counter_map_;
87 i::OS::MemoryMappedFile* Shell::counters_file_ = NULL;
88 CounterCollection Shell::local_counters_;
89 CounterCollection* Shell::counters_ = &local_counters_;
87 Persistent<Context> Shell::utility_context_; 90 Persistent<Context> Shell::utility_context_;
88 Persistent<Context> Shell::evaluation_context_; 91 Persistent<Context> Shell::evaluation_context_;
89 92
90 93
91 // Executes a string within the current v8 context. 94 // Executes a string within the current v8 context.
92 bool Shell::ExecuteString(Handle<String> source, 95 bool Shell::ExecuteString(Handle<String> source,
93 Handle<Value> name, 96 Handle<Value> name,
94 bool print_result, 97 bool print_result,
95 bool report_exceptions) { 98 bool report_exceptions) {
96 HandleScope handle_scope; 99 HandleScope handle_scope;
(...skipping 105 matching lines...) Expand 10 before | Expand all | Expand 10 after
202 Context::Scope context_scope(utility_context_); 205 Context::Scope context_scope(utility_context_);
203 Handle<Object> global = utility_context_->Global(); 206 Handle<Object> global = utility_context_->Global();
204 Handle<Value> fun = global->Get(String::New("GetCompletions")); 207 Handle<Value> fun = global->Get(String::New("GetCompletions"));
205 static const int kArgc = 3; 208 static const int kArgc = 3;
206 Handle<Value> argv[kArgc] = { evaluation_context_->Global(), text, full }; 209 Handle<Value> argv[kArgc] = { evaluation_context_->Global(), text, full };
207 Handle<Value> val = Handle<Function>::Cast(fun)->Call(global, kArgc, argv); 210 Handle<Value> val = Handle<Function>::Cast(fun)->Call(global, kArgc, argv);
208 return handle_scope.Close(Handle<Array>::Cast(val)); 211 return handle_scope.Close(Handle<Array>::Cast(val));
209 } 212 }
210 213
211 214
215 int32_t* Counter::Bind(const char* name) {
216 int i;
217 for (i = 0; i < kMaxNameSize - 1 && name[i]; i++)
218 name_[i] = static_cast<char>(name[i]);
219 name_[i] = '\0';
220 return &counter_;
221 }
222
223
224 CounterCollection::CounterCollection() {
225 magic_number_ = 0xDEADFACE;
226 max_counters_ = kMaxCounters;
227 max_name_size_ = Counter::kMaxNameSize;
228 counters_in_use_ = 0;
229 }
230
231
232 Counter* CounterCollection::GetNextCounter() {
233 if (counters_in_use_ == kMaxCounters) return NULL;
234 return &counters_[counters_in_use_++];
235 }
236
237
238 void Shell::MapCounters(const char* name) {
239 counters_file_ = i::OS::MemoryMappedFile::create(name,
240 sizeof(CounterCollection), &local_counters_);
241 void* memory = (counters_file_ == NULL) ?
242 NULL : counters_file_->memory();
243 if (memory == NULL) {
244 printf("Could not map counters file %s\n", name);
245 exit(1);
246 }
247 counters_ = static_cast<CounterCollection*>(memory);
248 V8::SetCounterFunction(LookupCounter);
249 }
250
251
212 int* Shell::LookupCounter(const char* name) { 252 int* Shell::LookupCounter(const char* name) {
213 CounterMap::iterator item = counter_map_.find(name); 253 CounterMap::iterator item = counter_map_.find(name);
214 if (item != counter_map_.end()) { 254 if (item != counter_map_.end()) {
215 Counter* result = (*item).second; 255 Counter* result = (*item).second;
216 return result->GetValuePtr(); 256 return result->ptr();
217 } 257 }
218 Counter* result = new Counter(name); 258 Counter* result = counters_->GetNextCounter();
219 counter_map_[name] = result; 259 if (result == NULL) return NULL;
220 return result->GetValuePtr(); 260 return result->Bind(name);
221 } 261 }
222 262
223 263
224 void Shell::Initialize() { 264 void Shell::Initialize() {
225 // Set up counters 265 // Set up counters
266 if (i::FLAG_map_counters != NULL)
267 MapCounters(i::FLAG_map_counters);
226 if (i::FLAG_dump_counters) 268 if (i::FLAG_dump_counters)
227 V8::SetCounterFunction(LookupCounter); 269 V8::SetCounterFunction(LookupCounter);
228 // Initialize the global objects 270 // Initialize the global objects
229 HandleScope scope; 271 HandleScope scope;
230 Handle<ObjectTemplate> global_template = ObjectTemplate::New(); 272 Handle<ObjectTemplate> global_template = ObjectTemplate::New();
231 global_template->Set(String::New("print"), FunctionTemplate::New(Print)); 273 global_template->Set(String::New("print"), FunctionTemplate::New(Print));
232 global_template->Set(String::New("load"), FunctionTemplate::New(Load)); 274 global_template->Set(String::New("load"), FunctionTemplate::New(Load));
233 global_template->Set(String::New("quit"), FunctionTemplate::New(Quit)); 275 global_template->Set(String::New("quit"), FunctionTemplate::New(Quit));
234 global_template->Set(String::New("version"), FunctionTemplate::New(Version)); 276 global_template->Set(String::New("version"), FunctionTemplate::New(Version));
235 277
(...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after
279 321
280 void Shell::OnExit() { 322 void Shell::OnExit() {
281 if (i::FLAG_dump_counters) { 323 if (i::FLAG_dump_counters) {
282 ::printf("+----------------------------------------+----------+\n"); 324 ::printf("+----------------------------------------+----------+\n");
283 ::printf("| Name | Value |\n"); 325 ::printf("| Name | Value |\n");
284 ::printf("+----------------------------------------+----------+\n"); 326 ::printf("+----------------------------------------+----------+\n");
285 for (CounterMap::iterator i = counter_map_.begin(); 327 for (CounterMap::iterator i = counter_map_.begin();
286 i != counter_map_.end(); 328 i != counter_map_.end();
287 i++) { 329 i++) {
288 Counter* counter = (*i).second; 330 Counter* counter = (*i).second;
289 ::printf("| %-38s | %8i |\n", counter->name(), counter->value()); 331 ::printf("| %-38s | %8i |\n", (*i).first, counter->value());
290 } 332 }
291 ::printf("+----------------------------------------+----------+\n"); 333 ::printf("+----------------------------------------+----------+\n");
292 } 334 }
335 if (counters_file_ != NULL)
336 delete counters_file_;
293 } 337 }
294 338
295 339
296 // Reads a file into a v8 string. 340 // Reads a file into a v8 string.
297 Handle<String> Shell::ReadFile(const char* name) { 341 Handle<String> Shell::ReadFile(const char* name) {
298 FILE* file = i::OS::FOpen(name, "rb"); 342 FILE* file = i::OS::FOpen(name, "rb");
299 if (file == NULL) return Handle<String>(); 343 if (file == NULL) return Handle<String>();
300 344
301 fseek(file, 0, SEEK_END); 345 fseek(file, 0, SEEK_END);
302 int size = ftell(file); 346 int size = ftell(file);
(...skipping 73 matching lines...) Expand 10 before | Expand all | Expand 10 after
376 return 0; 420 return 0;
377 } 421 }
378 422
379 423
380 } // namespace v8 424 } // namespace v8
381 425
382 426
383 int main(int argc, char* argv[]) { 427 int main(int argc, char* argv[]) {
384 return v8::Shell::Main(argc, argv); 428 return v8::Shell::Main(argc, argv);
385 } 429 }
OLDNEW
« no previous file with comments | « src/d8.h ('k') | src/flag-definitions.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698