Deleted Added
sdiff udiff text old ( 7010:c769c45253c9 ) new ( 7048:2ab58c54de63 )
full compact
1/*
2 * Copyright (c) 1999-2008 Mark D. Hill and David A. Wood
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are
7 * met: redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer;

--- 28 unchanged lines hidden (view full) ---

37
38 ----------------------------------------------------------------------
39
40 File modification date: 2008-02-23
41
42 ----------------------------------------------------------------------
43*/
44
45/*
46 * Profiler.cc
47 *
48 * Description: See Profiler.hh
49 *
50 * $Id$
51 *
52 */
53
54// Allows use of times() library call, which determines virtual runtime
55#include <sys/resource.h>
56#include <sys/times.h>
57
58#include "mem/ruby/profiler/Profiler.hh"
59#include "mem/ruby/profiler/AddressProfiler.hh"
60#include "mem/ruby/system/System.hh"
61#include "mem/ruby/network/Network.hh"
62#include "mem/gems_common/PrioHeap.hh"
63#include "mem/protocol/CacheMsg.hh"
64#include "mem/protocol/Protocol.hh"
65#include "mem/gems_common/util.hh"
66#include "mem/gems_common/Map.hh"
67#include "mem/ruby/common/Debug.hh"
68#include "mem/protocol/MachineType.hh"
69
70#include "mem/ruby/system/System.hh"
71
72extern std::ostream * debug_cout_ptr;
73
74static double process_memory_total();
75static double process_memory_resident();
76
77Profiler::Profiler(const Params *p)
78 : SimObject(p)
79{
80 m_requestProfileMap_ptr = new Map;
81
82 m_inst_profiler_ptr = NULL;
83 m_address_profiler_ptr = NULL;
84
85 m_real_time_start_time = time(NULL); // Not reset in clearStats()
86 m_stats_period = 1000000; // Default
87 m_periodic_output_file_ptr = &cerr;
88
89 m_hot_lines = p->hot_lines;
90 m_all_instructions = p->all_instructions;
91
92 m_num_of_sequencers = p->num_of_sequencers;
93
94 m_hot_lines = false;
95 m_all_instructions = false;
96
97 m_address_profiler_ptr = new AddressProfiler(m_num_of_sequencers);
98 m_address_profiler_ptr -> setHotLines(m_hot_lines);
99 m_address_profiler_ptr -> setAllInstructions(m_all_instructions);
100
101 if (m_all_instructions) {
102 m_inst_profiler_ptr = new AddressProfiler(m_num_of_sequencers);
103 m_inst_profiler_ptr -> setHotLines(m_hot_lines);
104 m_inst_profiler_ptr -> setAllInstructions(m_all_instructions);
105 }
106}
107
108Profiler::~Profiler()
109{
110 if (m_periodic_output_file_ptr != &cerr) {
111 delete m_periodic_output_file_ptr;
112 }
113
114 delete m_requestProfileMap_ptr;
115}
116
117void Profiler::wakeup()
118{
119 // FIXME - avoid the repeated code
120
121 Vector perProcCycleCount;
122 perProcCycleCount.setSize(m_num_of_sequencers);
123
124 for(int i=0; i < m_num_of_sequencers; i++) {
125 perProcCycleCount[i] = g_system_ptr->getCycleCount(i) - m_cycles_executed_at_start[i] + 1;
126 // The +1 allows us to avoid division by zero
127 }
128
129 (*m_periodic_output_file_ptr) << "ruby_cycles: "
130 << g_eventQueue_ptr->getTime()-m_ruby_start
131 << endl;
132
133 (*m_periodic_output_file_ptr) << "mbytes_resident: "
134 << process_memory_resident()
135 << endl;
136
137 (*m_periodic_output_file_ptr) << "mbytes_total: "
138 << process_memory_total()
139 << endl;
140
141 if (process_memory_total() > 0) {
142 (*m_periodic_output_file_ptr) << "resident_ratio: "
143 << process_memory_resident()/process_memory_total()
144 << endl;
145 }
146
147 (*m_periodic_output_file_ptr) << "miss_latency: "
148 << m_allMissLatencyHistogram
149 << endl;
150
151 *m_periodic_output_file_ptr << endl;
152
153 if (m_all_instructions) {
154 m_inst_profiler_ptr->printStats(*m_periodic_output_file_ptr);
155 }
156
157 //g_system_ptr->getNetwork()->printStats(*m_periodic_output_file_ptr);
158 g_eventQueue_ptr->scheduleEvent(this, m_stats_period);
159}
160
161void Profiler::setPeriodicStatsFile(const string& filename)
162{
163 cout << "Recording periodic statistics to file '" << filename << "' every "
164 << m_stats_period << " Ruby cycles" << endl;
165
166 if (m_periodic_output_file_ptr != &cerr) {
167 delete m_periodic_output_file_ptr;
168 }
169
170 m_periodic_output_file_ptr = new ofstream(filename.c_str());
171 g_eventQueue_ptr->scheduleEvent(this, 1);
172}
173
174void Profiler::setPeriodicStatsInterval(integer_t period)
175{
176 cout << "Recording periodic statistics every " << m_stats_period
177 << " Ruby cycles" << endl;
178
179 m_stats_period = period;
180 g_eventQueue_ptr->scheduleEvent(this, 1);
181}
182
183void Profiler::printConfig(ostream& out) const
184{
185 out << endl;
186 out << "Profiler Configuration" << endl;
187 out << "----------------------" << endl;
188 out << "periodic_stats_period: " << m_stats_period << endl;
189}
190
191void Profiler::print(ostream& out) const
192{
193 out << "[Profiler]";
194}
195
196void Profiler::printStats(ostream& out, bool short_stats)
197{
198 out << endl;
199 if (short_stats) {
200 out << "SHORT ";
201 }
202 out << "Profiler Stats" << endl;
203 out << "--------------" << endl;
204
205 time_t real_time_current = time(NULL);
206 double seconds = difftime(real_time_current, m_real_time_start_time);
207 double minutes = seconds/60.0;
208 double hours = minutes/60.0;
209 double days = hours/24.0;
210 Time ruby_cycles = g_eventQueue_ptr->getTime()-m_ruby_start;
211
212 if (!short_stats) {
213 out << "Elapsed_time_in_seconds: " << seconds << endl;
214 out << "Elapsed_time_in_minutes: " << minutes << endl;
215 out << "Elapsed_time_in_hours: " << hours << endl;
216 out << "Elapsed_time_in_days: " << days << endl;
217 out << endl;
218 }
219
220 // print the virtual runtimes as well
221 struct tms vtime;
222 times(&vtime);
223 seconds = (vtime.tms_utime + vtime.tms_stime) / 100.0;
224 minutes = seconds / 60.0;
225 hours = minutes / 60.0;
226 days = hours / 24.0;
227 out << "Virtual_time_in_seconds: " << seconds << endl;
228 out << "Virtual_time_in_minutes: " << minutes << endl;
229 out << "Virtual_time_in_hours: " << hours << endl;
230 out << "Virtual_time_in_days: " << days << endl;
231 out << endl;
232
233 out << "Ruby_current_time: " << g_eventQueue_ptr->getTime() << endl;
234 out << "Ruby_start_time: " << m_ruby_start << endl;
235 out << "Ruby_cycles: " << ruby_cycles << endl;
236 out << endl;
237
238 if (!short_stats) {
239 out << "mbytes_resident: " << process_memory_resident() << endl;
240 out << "mbytes_total: " << process_memory_total() << endl;
241 if (process_memory_total() > 0) {
242 out << "resident_ratio: "
243 << process_memory_resident()/process_memory_total() << endl;
244 }
245 out << endl;
246
247 }
248
249 Vector<integer_t> perProcCycleCount;
250 perProcCycleCount.setSize(m_num_of_sequencers);
251
252 for(int i=0; i < m_num_of_sequencers; i++) {
253 perProcCycleCount[i] = g_system_ptr->getCycleCount(i) - m_cycles_executed_at_start[i] + 1;
254 // The +1 allows us to avoid division by zero
255 }
256
257 out << "ruby_cycles_executed: " << perProcCycleCount << endl;
258
259 out << endl;
260
261 if (!short_stats) {
262 out << "Busy Controller Counts:" << endl;
263 for(int i=0; i < MachineType_NUM; i++) {
264 for(int j=0; j < MachineType_base_count((MachineType)i); j++) {
265 MachineID machID;
266 machID.type = (MachineType)i;
267 machID.num = j;
268 out << machID << ":" << m_busyControllerCount[i][j] << " ";
269 if ((j+1)%8 == 0) {
270 out << endl;
271 }
272 }
273 out << endl;
274 }
275 out << endl;
276
277 out << "Busy Bank Count:" << m_busyBankCount << endl;
278 out << endl;
279
280 out << "sequencer_requests_outstanding: " << m_sequencer_requests << endl;
281 out << endl;
282 }
283
284 if (!short_stats) {
285 out << "All Non-Zero Cycle Demand Cache Accesses" << endl;
286 out << "----------------------------------------" << endl;
287 out << "miss_latency: " << m_allMissLatencyHistogram << endl;
288 for(int i=0; i<m_missLatencyHistograms.size(); i++) {
289 if (m_missLatencyHistograms[i].size() > 0) {
290 out << "miss_latency_" << RubyRequestType(i) << ": " << m_missLatencyHistograms[i] << endl;
291 }
292 }
293 for(int i=0; i<m_machLatencyHistograms.size(); i++) {
294 if (m_machLatencyHistograms[i].size() > 0) {
295 out << "miss_latency_" << GenericMachineType(i) << ": " << m_machLatencyHistograms[i] << endl;
296 }
297 }
298
299 out << endl;
300
301 out << "All Non-Zero Cycle SW Prefetch Requests" << endl;
302 out << "------------------------------------" << endl;
303 out << "prefetch_latency: " << m_allSWPrefetchLatencyHistogram << endl;
304 for(int i=0; i<m_SWPrefetchLatencyHistograms.size(); i++) {
305 if (m_SWPrefetchLatencyHistograms[i].size() > 0) {
306 out << "prefetch_latency_" << CacheRequestType(i) << ": " << m_SWPrefetchLatencyHistograms[i] << endl;
307 }
308 }
309 for(int i=0; i<m_SWPrefetchMachLatencyHistograms.size(); i++) {
310 if (m_SWPrefetchMachLatencyHistograms[i].size() > 0) {
311 out << "prefetch_latency_" << GenericMachineType(i) << ": " << m_SWPrefetchMachLatencyHistograms[i] << endl;
312 }
313 }
314 out << "prefetch_latency_L2Miss:" << m_SWPrefetchL2MissLatencyHistogram << endl;
315
316 if (m_all_sharing_histogram.size() > 0) {
317 out << "all_sharing: " << m_all_sharing_histogram << endl;
318 out << "read_sharing: " << m_read_sharing_histogram << endl;
319 out << "write_sharing: " << m_write_sharing_histogram << endl;
320
321 out << "all_sharing_percent: "; m_all_sharing_histogram.printPercent(out); out << endl;
322 out << "read_sharing_percent: "; m_read_sharing_histogram.printPercent(out); out << endl;
323 out << "write_sharing_percent: "; m_write_sharing_histogram.printPercent(out); out << endl;
324
325 int64 total_miss = m_cache_to_cache + m_memory_to_cache;
326 out << "all_misses: " << total_miss << endl;
327 out << "cache_to_cache_misses: " << m_cache_to_cache << endl;
328 out << "memory_to_cache_misses: " << m_memory_to_cache << endl;
329 out << "cache_to_cache_percent: " << 100.0 * (double(m_cache_to_cache) / double(total_miss)) << endl;
330 out << "memory_to_cache_percent: " << 100.0 * (double(m_memory_to_cache) / double(total_miss)) << endl;
331 out << endl;
332 }
333
334 if (m_outstanding_requests.size() > 0) {
335 out << "outstanding_requests: "; m_outstanding_requests.printPercent(out); out << endl;
336 out << endl;
337 }
338 }
339
340 if (!short_stats) {
341 out << "Request vs. RubySystem State Profile" << endl;
342 out << "--------------------------------" << endl;
343 out << endl;
344
345 Vector<string> requestProfileKeys = m_requestProfileMap_ptr->keys();
346 requestProfileKeys.sortVector();
347
348 for(int i=0; i<requestProfileKeys.size(); i++) {
349 int temp_int = m_requestProfileMap_ptr->lookup(requestProfileKeys[i]);
350 double percent = (100.0*double(temp_int))/double(m_requests);
351 while (requestProfileKeys[i] != "") {
352 out << setw(10) << string_split(requestProfileKeys[i], ':');
353 }
354 out << setw(11) << temp_int;
355 out << setw(14) << percent << endl;
356 }
357 out << endl;
358
359 out << "filter_action: " << m_filter_action_histogram << endl;
360
361 if (!m_all_instructions) {
362 m_address_profiler_ptr->printStats(out);
363 }
364
365 if (m_all_instructions) {
366 m_inst_profiler_ptr->printStats(out);
367 }
368
369 out << endl;
370 out << "Message Delayed Cycles" << endl;
371 out << "----------------------" << endl;
372 out << "Total_delay_cycles: " << m_delayedCyclesHistogram << endl;
373 out << "Total_nonPF_delay_cycles: " << m_delayedCyclesNonPFHistogram << endl;
374 for (int i = 0; i < m_delayedCyclesVCHistograms.size(); i++) {
375 out << " virtual_network_" << i << "_delay_cycles: " << m_delayedCyclesVCHistograms[i] << endl;
376 }
377
378 printResourceUsage(out);
379 }
380
381}
382
383void Profiler::printResourceUsage(ostream& out) const
384{
385 out << endl;
386 out << "Resource Usage" << endl;
387 out << "--------------" << endl;
388
389 integer_t pagesize = getpagesize(); // page size in bytes
390 out << "page_size: " << pagesize << endl;
391
392 rusage usage;
393 getrusage (RUSAGE_SELF, &usage);
394
395 out << "user_time: " << usage.ru_utime.tv_sec << endl;
396 out << "system_time: " << usage.ru_stime.tv_sec << endl;
397 out << "page_reclaims: " << usage.ru_minflt << endl;
398 out << "page_faults: " << usage.ru_majflt << endl;
399 out << "swaps: " << usage.ru_nswap << endl;
400 out << "block_inputs: " << usage.ru_inblock << endl;
401 out << "block_outputs: " << usage.ru_oublock << endl;
402}
403
404void Profiler::clearStats()
405{
406 m_ruby_start = g_eventQueue_ptr->getTime();
407
408 m_cycles_executed_at_start.setSize(m_num_of_sequencers);
409 for (int i=0; i < m_num_of_sequencers; i++) {
410 if (g_system_ptr == NULL) {
411 m_cycles_executed_at_start[i] = 0;
412 } else {
413 m_cycles_executed_at_start[i] = g_system_ptr->getCycleCount(i);
414 }
415 }
416
417 m_busyControllerCount.setSize(MachineType_NUM); // all machines
418 for(int i=0; i < MachineType_NUM; i++) {
419 m_busyControllerCount[i].setSize(MachineType_base_count((MachineType)i));
420 for(int j=0; j < MachineType_base_count((MachineType)i); j++) {
421 m_busyControllerCount[i][j] = 0;
422 }
423 }
424 m_busyBankCount = 0;
425
426 m_delayedCyclesHistogram.clear();
427 m_delayedCyclesNonPFHistogram.clear();
428 m_delayedCyclesVCHistograms.setSize(RubySystem::getNetwork()->getNumberOfVirtualNetworks());
429 for (int i = 0; i < RubySystem::getNetwork()->getNumberOfVirtualNetworks(); i++) {
430 m_delayedCyclesVCHistograms[i].clear();
431 }
432
433 m_missLatencyHistograms.setSize(RubyRequestType_NUM);
434 for(int i=0; i<m_missLatencyHistograms.size(); i++) {
435 m_missLatencyHistograms[i].clear(200);
436 }
437 m_machLatencyHistograms.setSize(GenericMachineType_NUM+1);
438 for(int i=0; i<m_machLatencyHistograms.size(); i++) {
439 m_machLatencyHistograms[i].clear(200);
440 }
441 m_allMissLatencyHistogram.clear(200);
442
443 m_SWPrefetchLatencyHistograms.setSize(CacheRequestType_NUM);
444 for(int i=0; i<m_SWPrefetchLatencyHistograms.size(); i++) {
445 m_SWPrefetchLatencyHistograms[i].clear(200);
446 }
447 m_SWPrefetchMachLatencyHistograms.setSize(GenericMachineType_NUM+1);
448 for(int i=0; i<m_SWPrefetchMachLatencyHistograms.size(); i++) {
449 m_SWPrefetchMachLatencyHistograms[i].clear(200);
450 }
451 m_allSWPrefetchLatencyHistogram.clear(200);
452
453 m_sequencer_requests.clear();
454 m_read_sharing_histogram.clear();
455 m_write_sharing_histogram.clear();
456 m_all_sharing_histogram.clear();
457 m_cache_to_cache = 0;
458 m_memory_to_cache = 0;
459
460 // clear HashMaps
461 m_requestProfileMap_ptr->clear();
462
463 // count requests profiled
464 m_requests = 0;
465
466 m_outstanding_requests.clear();
467 m_outstanding_persistent_requests.clear();
468
469 // Flush the prefetches through the system - used so that there are no outstanding requests after stats are cleared
470 //g_eventQueue_ptr->triggerAllEvents();
471
472 // update the start time
473 m_ruby_start = g_eventQueue_ptr->getTime();
474}
475
476void Profiler::addAddressTraceSample(const CacheMsg& msg, NodeID id)
477{
478 if (msg.getType() != CacheRequestType_IFETCH) {
479
480 // Note: The following line should be commented out if you want to
481 // use the special profiling that is part of the GS320 protocol
482
483 // NOTE: Unless PROFILE_HOT_LINES is enabled, nothing will be profiled by the AddressProfiler
484 m_address_profiler_ptr->addTraceSample(msg.getLineAddress(), msg.getProgramCounter(), msg.getType(), msg.getAccessMode(), id, false);
485 }
486}
487
488void Profiler::profileSharing(const Address& addr, AccessType type, NodeID requestor, const Set& sharers, const Set& owner)
489{
490 Set set_contacted(owner);
491 if (type == AccessType_Write) {
492 set_contacted.addSet(sharers);
493 }
494 set_contacted.remove(requestor);
495 int number_contacted = set_contacted.count();
496
497 if (type == AccessType_Write) {
498 m_write_sharing_histogram.add(number_contacted);
499 } else {
500 m_read_sharing_histogram.add(number_contacted);
501 }
502 m_all_sharing_histogram.add(number_contacted);
503
504 if (number_contacted == 0) {
505 m_memory_to_cache++;
506 } else {
507 m_cache_to_cache++;
508 }
509
510}
511
512void Profiler::profileMsgDelay(int virtualNetwork, int delayCycles) {
513 assert(virtualNetwork < m_delayedCyclesVCHistograms.size());
514 m_delayedCyclesHistogram.add(delayCycles);
515 m_delayedCyclesVCHistograms[virtualNetwork].add(delayCycles);
516 if (virtualNetwork != 0) {
517 m_delayedCyclesNonPFHistogram.add(delayCycles);
518 }
519}
520
521// profiles original cache requests including PUTs
522void Profiler::profileRequest(const string& requestStr)
523{
524 m_requests++;
525
526 if (m_requestProfileMap_ptr->exist(requestStr)) {
527 (m_requestProfileMap_ptr->lookup(requestStr))++;
528 } else {
529 m_requestProfileMap_ptr->add(requestStr, 1);
530 }
531}
532
533void Profiler::controllerBusy(MachineID machID)
534{
535 m_busyControllerCount[(int)machID.type][(int)machID.num]++;
536}
537
538void Profiler::profilePFWait(Time waitTime)
539{
540 m_prefetchWaitHistogram.add(waitTime);
541}
542
543void Profiler::bankBusy()
544{
545 m_busyBankCount++;
546}
547
548// non-zero cycle demand request
549void Profiler::missLatency(Time t, RubyRequestType type)
550{
551 m_allMissLatencyHistogram.add(t);
552 m_missLatencyHistograms[type].add(t);
553}
554
555// non-zero cycle prefetch request
556void Profiler::swPrefetchLatency(Time t, CacheRequestType type, GenericMachineType respondingMach)
557{
558 m_allSWPrefetchLatencyHistogram.add(t);
559 m_SWPrefetchLatencyHistograms[type].add(t);
560 m_SWPrefetchMachLatencyHistograms[respondingMach].add(t);
561 if(respondingMach == GenericMachineType_Directory || respondingMach == GenericMachineType_NUM) {
562 m_SWPrefetchL2MissLatencyHistogram.add(t);
563 }
564}
565
566void Profiler::profileTransition(const string& component, NodeID version, Address addr,
567 const string& state, const string& event,
568 const string& next_state, const string& note)
569{
570 const int EVENT_SPACES = 20;
571 const int ID_SPACES = 3;
572 const int TIME_SPACES = 7;
573 const int COMP_SPACES = 10;
574 const int STATE_SPACES = 6;
575
576 if ((g_debug_ptr->getDebugTime() > 0) &&
577 (g_eventQueue_ptr->getTime() >= g_debug_ptr->getDebugTime())) {
578 (* debug_cout_ptr).flags(ios::right);
579 (* debug_cout_ptr) << setw(TIME_SPACES) << g_eventQueue_ptr->getTime() << " ";
580 (* debug_cout_ptr) << setw(ID_SPACES) << version << " ";
581 (* debug_cout_ptr) << setw(COMP_SPACES) << component;
582 (* debug_cout_ptr) << setw(EVENT_SPACES) << event << " ";
583
584 (* debug_cout_ptr).flags(ios::right);
585 (* debug_cout_ptr) << setw(STATE_SPACES) << state;
586 (* debug_cout_ptr) << ">";
587 (* debug_cout_ptr).flags(ios::left);
588 (* debug_cout_ptr) << setw(STATE_SPACES) << next_state;
589
590 (* debug_cout_ptr) << " " << addr << " " << note;
591
592 (* debug_cout_ptr) << endl;
593 }
594}
595
596// Helper function
597static double process_memory_total()
598{
599 const double MULTIPLIER = 4096.0/(1024.0*1024.0); // 4kB page size, 1024*1024 bytes per MB,
600 ifstream proc_file;
601 proc_file.open("/proc/self/statm");
602 int total_size_in_pages = 0;
603 int res_size_in_pages = 0;
604 proc_file >> total_size_in_pages;
605 proc_file >> res_size_in_pages;
606 return double(total_size_in_pages)*MULTIPLIER; // size in megabytes
607}
608
609static double process_memory_resident()
610{
611 const double MULTIPLIER = 4096.0/(1024.0*1024.0); // 4kB page size, 1024*1024 bytes per MB,
612 ifstream proc_file;
613 proc_file.open("/proc/self/statm");
614 int total_size_in_pages = 0;
615 int res_size_in_pages = 0;
616 proc_file >> total_size_in_pages;
617 proc_file >> res_size_in_pages;
618 return double(res_size_in_pages)*MULTIPLIER; // size in megabytes
619}
620
621void Profiler::rubyWatch(int id){
622 uint64 tr = 0;
623 Address watch_address = Address(tr);
624 const int ID_SPACES = 3;
625 const int TIME_SPACES = 7;
626
627 (* debug_cout_ptr).flags(ios::right);
628 (* debug_cout_ptr) << setw(TIME_SPACES) << g_eventQueue_ptr->getTime() << " ";
629 (* debug_cout_ptr) << setw(ID_SPACES) << id << " "
630 << "RUBY WATCH "
631 << watch_address
632 << endl;
633
634 if(!m_watch_address_list_ptr->exist(watch_address)){
635 m_watch_address_list_ptr->add(watch_address, 1);
636 }
637}
638
639bool Profiler::watchAddress(Address addr){
640 if (m_watch_address_list_ptr->exist(addr))
641 return true;
642 else
643 return false;
644}
645
646Profiler *
647RubyProfilerParams::create()
648{
649 return new Profiler(this);
650}