Profiler.cc revision 6897:cfeb3d9563dd
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;
9 * redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution;
12 * neither the name of the copyright holders nor the names of its
13 * contributors may be used to endorse or promote products derived from
14 * this software without specific prior written permission.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
19 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
20 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 */
28
29/*
30   This file has been modified by Kevin Moore and Dan Nussbaum of the
31   Scalable Systems Research Group at Sun Microsystems Laboratories
32   (http://research.sun.com/scalable/) to support the Adaptive
33   Transactional Memory Test Platform (ATMTP).
34
35   Please send email to atmtp-interest@sun.com with feedback, questions, or
36   to request future announcements about ATMTP.
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#include "mem/ruby/profiler/Profiler.hh"
55#include "mem/ruby/profiler/AddressProfiler.hh"
56#include "mem/ruby/system/System.hh"
57#include "mem/ruby/network/Network.hh"
58#include "mem/gems_common/PrioHeap.hh"
59#include "mem/protocol/CacheMsg.hh"
60#include "mem/protocol/Protocol.hh"
61#include "mem/gems_common/util.hh"
62#include "mem/gems_common/Map.hh"
63#include "mem/ruby/common/Debug.hh"
64#include "mem/protocol/MachineType.hh"
65
66#include "mem/ruby/system/System.hh"
67
68// Allows use of times() library call, which determines virtual runtime
69#include <sys/times.h>
70
71extern std::ostream * debug_cout_ptr;
72
73static double process_memory_total();
74static double process_memory_resident();
75
76Profiler::Profiler(const Params *p)
77    : SimObject(p)
78{
79  m_requestProfileMap_ptr = new Map<string, int>;
80
81  m_inst_profiler_ptr = NULL;
82  m_address_profiler_ptr = NULL;
83
84  m_real_time_start_time = time(NULL); // Not reset in clearStats()
85  m_stats_period = 1000000; // Default
86  m_periodic_output_file_ptr = &cerr;
87
88  m_hot_lines = p->hot_lines;
89  m_all_instructions = p->all_instructions;
90
91  m_num_of_sequencers = p->num_of_sequencers;
92
93  m_hot_lines = false;
94  m_all_instructions = false;
95
96  m_address_profiler_ptr = new AddressProfiler(m_num_of_sequencers);
97  m_address_profiler_ptr -> setHotLines(m_hot_lines);
98  m_address_profiler_ptr -> setAllInstructions(m_all_instructions);
99
100  if (m_all_instructions) {
101    m_inst_profiler_ptr = new AddressProfiler(m_num_of_sequencers);
102    m_inst_profiler_ptr -> setHotLines(m_hot_lines);
103    m_inst_profiler_ptr -> setAllInstructions(m_all_instructions);
104  }
105}
106
107Profiler::~Profiler()
108{
109  if (m_periodic_output_file_ptr != &cerr) {
110    delete m_periodic_output_file_ptr;
111  }
112
113  delete m_requestProfileMap_ptr;
114}
115
116void Profiler::wakeup()
117{
118  // FIXME - avoid the repeated code
119
120  Vector<integer_t> perProcCycleCount;
121  perProcCycleCount.setSize(m_num_of_sequencers);
122
123  for(int i=0; i < m_num_of_sequencers; i++) {
124    perProcCycleCount[i] = g_system_ptr->getCycleCount(i) - m_cycles_executed_at_start[i] + 1;
125    // The +1 allows us to avoid division by zero
126  }
127
128  integer_t total_misses = m_perProcTotalMisses.sum();
129  integer_t simics_cycles_executed = perProcCycleCount.sum();
130  integer_t transactions_started = m_perProcStartTransaction.sum();
131  integer_t transactions_ended = m_perProcEndTransaction.sum();
132
133  (*m_periodic_output_file_ptr) << "ruby_cycles: "
134                                << g_eventQueue_ptr->getTime()-m_ruby_start
135                                << endl;
136
137  (*m_periodic_output_file_ptr) << "total_misses: "
138                                << total_misses
139                                << " "
140                                << m_perProcTotalMisses
141                                << endl;
142
143  (*m_periodic_output_file_ptr) << "simics_cycles_executed: "
144                                << simics_cycles_executed
145                                << " "
146                                << perProcCycleCount
147                                << endl;
148
149  (*m_periodic_output_file_ptr) << "transactions_started: "
150                                << transactions_started
151                                << " "
152                                << m_perProcStartTransaction
153                                << endl;
154
155  (*m_periodic_output_file_ptr) << "transactions_ended: "
156                                << transactions_ended
157                                << " "
158                                << m_perProcEndTransaction
159                                << endl;
160
161  (*m_periodic_output_file_ptr) << "mbytes_resident: "
162                                << process_memory_resident()
163                                << endl;
164
165  (*m_periodic_output_file_ptr) << "mbytes_total: "
166                                << process_memory_total()
167                                << endl;
168
169  if (process_memory_total() > 0) {
170    (*m_periodic_output_file_ptr) << "resident_ratio: "
171                          << process_memory_resident()/process_memory_total()
172                          << endl;
173  }
174
175  (*m_periodic_output_file_ptr) << "miss_latency: "
176                                << m_allMissLatencyHistogram
177                                << endl;
178
179  *m_periodic_output_file_ptr << endl;
180
181  if (m_all_instructions) {
182    m_inst_profiler_ptr->printStats(*m_periodic_output_file_ptr);
183  }
184
185  //g_system_ptr->getNetwork()->printStats(*m_periodic_output_file_ptr);
186  g_eventQueue_ptr->scheduleEvent(this, m_stats_period);
187}
188
189void Profiler::setPeriodicStatsFile(const string& filename)
190{
191  cout << "Recording periodic statistics to file '" << filename << "' every "
192       << m_stats_period << " Ruby cycles" << endl;
193
194  if (m_periodic_output_file_ptr != &cerr) {
195    delete m_periodic_output_file_ptr;
196  }
197
198  m_periodic_output_file_ptr = new ofstream(filename.c_str());
199  g_eventQueue_ptr->scheduleEvent(this, 1);
200}
201
202void Profiler::setPeriodicStatsInterval(integer_t period)
203{
204  cout << "Recording periodic statistics every " << m_stats_period
205       << " Ruby cycles" << endl;
206
207  m_stats_period = period;
208  g_eventQueue_ptr->scheduleEvent(this, 1);
209}
210
211void Profiler::printConfig(ostream& out) const
212{
213  out << endl;
214  out << "Profiler Configuration" << endl;
215  out << "----------------------" << endl;
216  out << "periodic_stats_period: " << m_stats_period << endl;
217}
218
219void Profiler::print(ostream& out) const
220{
221  out << "[Profiler]";
222}
223
224void Profiler::printStats(ostream& out, bool short_stats)
225{
226  out << endl;
227  if (short_stats) {
228    out << "SHORT ";
229  }
230  out << "Profiler Stats" << endl;
231  out << "--------------" << endl;
232
233  time_t real_time_current = time(NULL);
234  double seconds = difftime(real_time_current, m_real_time_start_time);
235  double minutes = seconds/60.0;
236  double hours = minutes/60.0;
237  double days = hours/24.0;
238  Time ruby_cycles = g_eventQueue_ptr->getTime()-m_ruby_start;
239
240  if (!short_stats) {
241    out << "Elapsed_time_in_seconds: " << seconds << endl;
242    out << "Elapsed_time_in_minutes: " << minutes << endl;
243    out << "Elapsed_time_in_hours: " << hours << endl;
244    out << "Elapsed_time_in_days: " << days << endl;
245    out << endl;
246  }
247
248  // print the virtual runtimes as well
249  struct tms vtime;
250  times(&vtime);
251  seconds = (vtime.tms_utime + vtime.tms_stime) / 100.0;
252  minutes = seconds / 60.0;
253  hours = minutes / 60.0;
254  days = hours / 24.0;
255  out << "Virtual_time_in_seconds: " << seconds << endl;
256  out << "Virtual_time_in_minutes: " << minutes << endl;
257  out << "Virtual_time_in_hours:   " << hours << endl;
258  out << "Virtual_time_in_days:    " << days << endl;
259  out << endl;
260
261  out << "Ruby_current_time: " << g_eventQueue_ptr->getTime() << endl;
262  out << "Ruby_start_time: " << m_ruby_start << endl;
263  out << "Ruby_cycles: " << ruby_cycles << endl;
264  out << endl;
265
266  if (!short_stats) {
267    out << "mbytes_resident: " << process_memory_resident() << endl;
268    out << "mbytes_total: " << process_memory_total() << endl;
269    if (process_memory_total() > 0) {
270      out << "resident_ratio: "
271          << process_memory_resident()/process_memory_total() << endl;
272    }
273    out << endl;
274
275  }
276
277  Vector<integer_t> perProcCycleCount;
278  Vector<double> perProcCyclesPerTrans;
279  Vector<double> perProcMissesPerTrans;
280
281
282  perProcCycleCount.setSize(m_num_of_sequencers);
283  perProcCyclesPerTrans.setSize(m_num_of_sequencers);
284  perProcMissesPerTrans.setSize(m_num_of_sequencers);
285
286  for(int i=0; i < m_num_of_sequencers; i++) {
287    perProcCycleCount[i] = g_system_ptr->getCycleCount(i) - m_cycles_executed_at_start[i] + 1;
288    // The +1 allows us to avoid division by zero
289
290    int trans = m_perProcEndTransaction[i];
291    if (trans == 0) {
292      perProcCyclesPerTrans[i] = 0;
293      perProcMissesPerTrans[i] = 0;
294    } else {
295      perProcCyclesPerTrans[i] = ruby_cycles / double(trans);
296      perProcMissesPerTrans[i] = m_perProcTotalMisses[i] / double(trans);
297    }
298  }
299
300  integer_t total_misses = m_perProcTotalMisses.sum();
301  integer_t user_misses = m_perProcUserMisses.sum();
302  integer_t supervisor_misses = m_perProcSupervisorMisses.sum();
303  integer_t simics_cycles_executed = perProcCycleCount.sum();
304  integer_t transactions_started = m_perProcStartTransaction.sum();
305  integer_t transactions_ended = m_perProcEndTransaction.sum();
306
307  double cycles_per_transaction = (transactions_ended != 0) ? (m_num_of_sequencers * double(ruby_cycles)) / double(transactions_ended) : 0;
308  double misses_per_transaction = (transactions_ended != 0) ? double(total_misses) / double(transactions_ended) : 0;
309
310  out << "Total_misses: " << total_misses << endl;
311  out << "total_misses: " << total_misses << " " << m_perProcTotalMisses << endl;
312  out << "user_misses: " << user_misses << " " << m_perProcUserMisses << endl;
313  out << "supervisor_misses: " << supervisor_misses << " " << m_perProcSupervisorMisses << endl;
314  out << endl;
315  out << "ruby_cycles_executed: " << simics_cycles_executed << " " << perProcCycleCount << endl;
316  out << endl;
317  out << "transactions_started: " << transactions_started << " " << m_perProcStartTransaction << endl;
318  out << "transactions_ended: " << transactions_ended << " " << m_perProcEndTransaction << endl;
319  out << "cycles_per_transaction: " << cycles_per_transaction  << " " << perProcCyclesPerTrans << endl;
320  out << "misses_per_transaction: " << misses_per_transaction << " " << perProcMissesPerTrans << endl;
321
322  out << endl;
323
324  out << endl;
325
326  if (!short_stats) {
327    out << "Busy Controller Counts:" << endl;
328    for(int i=0; i < MachineType_NUM; i++) {
329      for(int j=0; j < MachineType_base_count((MachineType)i); j++) {
330        MachineID machID;
331        machID.type = (MachineType)i;
332        machID.num = j;
333        out << machID << ":" << m_busyControllerCount[i][j] << "  ";
334        if ((j+1)%8 == 0) {
335          out << endl;
336        }
337      }
338      out << endl;
339    }
340    out << endl;
341
342    out << "Busy Bank Count:" << m_busyBankCount << endl;
343    out << endl;
344
345    out << "sequencer_requests_outstanding: " << m_sequencer_requests << endl;
346    out << endl;
347  }
348
349  if (!short_stats) {
350    out << "All Non-Zero Cycle Demand Cache Accesses" << endl;
351    out << "----------------------------------------" << endl;
352    out << "miss_latency: " << m_allMissLatencyHistogram << endl;
353    for(int i=0; i<m_missLatencyHistograms.size(); i++) {
354      if (m_missLatencyHistograms[i].size() > 0) {
355        out << "miss_latency_" << RubyRequestType(i) << ": " << m_missLatencyHistograms[i] << endl;
356      }
357    }
358    for(int i=0; i<m_machLatencyHistograms.size(); i++) {
359      if (m_machLatencyHistograms[i].size() > 0) {
360        out << "miss_latency_" << GenericMachineType(i) << ": " << m_machLatencyHistograms[i] << endl;
361      }
362    }
363
364    out << endl;
365
366    out << "All Non-Zero Cycle SW Prefetch Requests" << endl;
367    out << "------------------------------------" << endl;
368    out << "prefetch_latency: " << m_allSWPrefetchLatencyHistogram << endl;
369    for(int i=0; i<m_SWPrefetchLatencyHistograms.size(); i++) {
370      if (m_SWPrefetchLatencyHistograms[i].size() > 0) {
371        out << "prefetch_latency_" << CacheRequestType(i) << ": " << m_SWPrefetchLatencyHistograms[i] << endl;
372      }
373    }
374    for(int i=0; i<m_SWPrefetchMachLatencyHistograms.size(); i++) {
375      if (m_SWPrefetchMachLatencyHistograms[i].size() > 0) {
376        out << "prefetch_latency_" << GenericMachineType(i) << ": " << m_SWPrefetchMachLatencyHistograms[i] << endl;
377      }
378    }
379    out << "prefetch_latency_L2Miss:" << m_SWPrefetchL2MissLatencyHistogram << endl;
380
381    if (m_all_sharing_histogram.size() > 0) {
382      out << "all_sharing: " << m_all_sharing_histogram << endl;
383      out << "read_sharing: " << m_read_sharing_histogram << endl;
384      out << "write_sharing: " << m_write_sharing_histogram << endl;
385
386      out << "all_sharing_percent: "; m_all_sharing_histogram.printPercent(out); out << endl;
387      out << "read_sharing_percent: "; m_read_sharing_histogram.printPercent(out); out << endl;
388      out << "write_sharing_percent: "; m_write_sharing_histogram.printPercent(out); out << endl;
389
390      int64 total_miss = m_cache_to_cache +  m_memory_to_cache;
391      out << "all_misses: " << total_miss << endl;
392      out << "cache_to_cache_misses: " << m_cache_to_cache << endl;
393      out << "memory_to_cache_misses: " << m_memory_to_cache << endl;
394      out << "cache_to_cache_percent: " << 100.0 * (double(m_cache_to_cache) / double(total_miss)) << endl;
395      out << "memory_to_cache_percent: " << 100.0 * (double(m_memory_to_cache) / double(total_miss)) << endl;
396      out << endl;
397    }
398
399    if (m_outstanding_requests.size() > 0) {
400      out << "outstanding_requests: "; m_outstanding_requests.printPercent(out); out << endl;
401      out << endl;
402    }
403  }
404
405  if (!short_stats) {
406    out << "Request vs. RubySystem State Profile" << endl;
407    out << "--------------------------------" << endl;
408    out << endl;
409
410    Vector<string> requestProfileKeys = m_requestProfileMap_ptr->keys();
411    requestProfileKeys.sortVector();
412
413    for(int i=0; i<requestProfileKeys.size(); i++) {
414      int temp_int = m_requestProfileMap_ptr->lookup(requestProfileKeys[i]);
415      double percent = (100.0*double(temp_int))/double(m_requests);
416      while (requestProfileKeys[i] != "") {
417        out << setw(10) << string_split(requestProfileKeys[i], ':');
418      }
419      out << setw(11) << temp_int;
420      out << setw(14) << percent << endl;
421    }
422    out << endl;
423
424    out << "filter_action: " << m_filter_action_histogram << endl;
425
426    if (!m_all_instructions) {
427      m_address_profiler_ptr->printStats(out);
428    }
429
430    if (m_all_instructions) {
431      m_inst_profiler_ptr->printStats(out);
432    }
433
434    out << endl;
435    out << "Message Delayed Cycles" << endl;
436    out << "----------------------" << endl;
437    out << "Total_delay_cycles: " <<   m_delayedCyclesHistogram << endl;
438    out << "Total_nonPF_delay_cycles: " << m_delayedCyclesNonPFHistogram << endl;
439    for (int i = 0; i < m_delayedCyclesVCHistograms.size(); i++) {
440      out << "  virtual_network_" << i << "_delay_cycles: " << m_delayedCyclesVCHistograms[i] << endl;
441    }
442
443    printResourceUsage(out);
444  }
445
446}
447
448void Profiler::printResourceUsage(ostream& out) const
449{
450  out << endl;
451  out << "Resource Usage" << endl;
452  out << "--------------" << endl;
453
454  integer_t pagesize = getpagesize(); // page size in bytes
455  out << "page_size: " << pagesize << endl;
456
457  rusage usage;
458  getrusage (RUSAGE_SELF, &usage);
459
460  out << "user_time: " << usage.ru_utime.tv_sec << endl;
461  out << "system_time: " << usage.ru_stime.tv_sec << endl;
462  out << "page_reclaims: " << usage.ru_minflt << endl;
463  out << "page_faults: " << usage.ru_majflt << endl;
464  out << "swaps: " << usage.ru_nswap << endl;
465  out << "block_inputs: " << usage.ru_inblock << endl;
466  out << "block_outputs: " << usage.ru_oublock << endl;
467}
468
469void Profiler::clearStats()
470{
471  m_ruby_start = g_eventQueue_ptr->getTime();
472
473  m_cycles_executed_at_start.setSize(m_num_of_sequencers);
474  for (int i=0; i < m_num_of_sequencers; i++) {
475    if (g_system_ptr == NULL) {
476      m_cycles_executed_at_start[i] = 0;
477    } else {
478      m_cycles_executed_at_start[i] = g_system_ptr->getCycleCount(i);
479    }
480  }
481
482  m_perProcTotalMisses.setSize(m_num_of_sequencers);
483  m_perProcUserMisses.setSize(m_num_of_sequencers);
484  m_perProcSupervisorMisses.setSize(m_num_of_sequencers);
485  m_perProcStartTransaction.setSize(m_num_of_sequencers);
486  m_perProcEndTransaction.setSize(m_num_of_sequencers);
487
488  for(int i=0; i < m_num_of_sequencers; i++) {
489    m_perProcTotalMisses[i] = 0;
490    m_perProcUserMisses[i] = 0;
491    m_perProcSupervisorMisses[i] = 0;
492    m_perProcStartTransaction[i] = 0;
493    m_perProcEndTransaction[i] = 0;
494  }
495
496  m_busyControllerCount.setSize(MachineType_NUM); // all machines
497  for(int i=0; i < MachineType_NUM; i++) {
498    m_busyControllerCount[i].setSize(MachineType_base_count((MachineType)i));
499    for(int j=0; j < MachineType_base_count((MachineType)i); j++) {
500      m_busyControllerCount[i][j] = 0;
501    }
502  }
503  m_busyBankCount = 0;
504
505  m_delayedCyclesHistogram.clear();
506  m_delayedCyclesNonPFHistogram.clear();
507  m_delayedCyclesVCHistograms.setSize(RubySystem::getNetwork()->getNumberOfVirtualNetworks());
508  for (int i = 0; i < RubySystem::getNetwork()->getNumberOfVirtualNetworks(); i++) {
509    m_delayedCyclesVCHistograms[i].clear();
510  }
511
512  m_missLatencyHistograms.setSize(RubyRequestType_NUM);
513  for(int i=0; i<m_missLatencyHistograms.size(); i++) {
514    m_missLatencyHistograms[i].clear(200);
515  }
516  m_machLatencyHistograms.setSize(GenericMachineType_NUM+1);
517  for(int i=0; i<m_machLatencyHistograms.size(); i++) {
518    m_machLatencyHistograms[i].clear(200);
519  }
520  m_allMissLatencyHistogram.clear(200);
521
522  m_SWPrefetchLatencyHistograms.setSize(CacheRequestType_NUM);
523  for(int i=0; i<m_SWPrefetchLatencyHistograms.size(); i++) {
524    m_SWPrefetchLatencyHistograms[i].clear(200);
525  }
526  m_SWPrefetchMachLatencyHistograms.setSize(GenericMachineType_NUM+1);
527  for(int i=0; i<m_SWPrefetchMachLatencyHistograms.size(); i++) {
528    m_SWPrefetchMachLatencyHistograms[i].clear(200);
529  }
530  m_allSWPrefetchLatencyHistogram.clear(200);
531
532  m_sequencer_requests.clear();
533  m_read_sharing_histogram.clear();
534  m_write_sharing_histogram.clear();
535  m_all_sharing_histogram.clear();
536  m_cache_to_cache = 0;
537  m_memory_to_cache = 0;
538
539  // clear HashMaps
540  m_requestProfileMap_ptr->clear();
541
542  // count requests profiled
543  m_requests = 0;
544
545  m_outstanding_requests.clear();
546  m_outstanding_persistent_requests.clear();
547
548  // Flush the prefetches through the system - used so that there are no outstanding requests after stats are cleared
549  //g_eventQueue_ptr->triggerAllEvents();
550
551  // update the start time
552  m_ruby_start = g_eventQueue_ptr->getTime();
553}
554
555void Profiler::addAddressTraceSample(const CacheMsg& msg, NodeID id)
556{
557  if (msg.getType() != CacheRequestType_IFETCH) {
558
559    // Note: The following line should be commented out if you want to
560    // use the special profiling that is part of the GS320 protocol
561
562    // NOTE: Unless PROFILE_HOT_LINES is enabled, nothing will be profiled by the AddressProfiler
563    m_address_profiler_ptr->addTraceSample(msg.getLineAddress(), msg.getProgramCounter(), msg.getType(), msg.getAccessMode(), id, false);
564  }
565}
566
567void Profiler::profileSharing(const Address& addr, AccessType type, NodeID requestor, const Set& sharers, const Set& owner)
568{
569  Set set_contacted(owner);
570  if (type == AccessType_Write) {
571    set_contacted.addSet(sharers);
572  }
573  set_contacted.remove(requestor);
574  int number_contacted = set_contacted.count();
575
576  if (type == AccessType_Write) {
577    m_write_sharing_histogram.add(number_contacted);
578  } else {
579    m_read_sharing_histogram.add(number_contacted);
580  }
581  m_all_sharing_histogram.add(number_contacted);
582
583  if (number_contacted == 0) {
584    m_memory_to_cache++;
585  } else {
586    m_cache_to_cache++;
587  }
588
589}
590
591void Profiler::profileMsgDelay(int virtualNetwork, int delayCycles) {
592  assert(virtualNetwork < m_delayedCyclesVCHistograms.size());
593  m_delayedCyclesHistogram.add(delayCycles);
594  m_delayedCyclesVCHistograms[virtualNetwork].add(delayCycles);
595  if (virtualNetwork != 0) {
596    m_delayedCyclesNonPFHistogram.add(delayCycles);
597  }
598}
599
600// profiles original cache requests including PUTs
601void Profiler::profileRequest(const string& requestStr)
602{
603  m_requests++;
604
605  if (m_requestProfileMap_ptr->exist(requestStr)) {
606    (m_requestProfileMap_ptr->lookup(requestStr))++;
607  } else {
608    m_requestProfileMap_ptr->add(requestStr, 1);
609  }
610}
611
612void Profiler::startTransaction(int cpu)
613{
614  m_perProcStartTransaction[cpu]++;
615}
616
617void Profiler::endTransaction(int cpu)
618{
619  m_perProcEndTransaction[cpu]++;
620}
621
622void Profiler::controllerBusy(MachineID machID)
623{
624  m_busyControllerCount[(int)machID.type][(int)machID.num]++;
625}
626
627void Profiler::profilePFWait(Time waitTime)
628{
629  m_prefetchWaitHistogram.add(waitTime);
630}
631
632void Profiler::bankBusy()
633{
634  m_busyBankCount++;
635}
636
637// non-zero cycle demand request
638void Profiler::missLatency(Time t, RubyRequestType type)
639{
640  m_allMissLatencyHistogram.add(t);
641  m_missLatencyHistograms[type].add(t);
642}
643
644// non-zero cycle prefetch request
645void Profiler::swPrefetchLatency(Time t, CacheRequestType type, GenericMachineType respondingMach)
646{
647  m_allSWPrefetchLatencyHistogram.add(t);
648  m_SWPrefetchLatencyHistograms[type].add(t);
649  m_SWPrefetchMachLatencyHistograms[respondingMach].add(t);
650  if(respondingMach == GenericMachineType_Directory || respondingMach == GenericMachineType_NUM) {
651    m_SWPrefetchL2MissLatencyHistogram.add(t);
652  }
653}
654
655void Profiler::profileTransition(const string& component, NodeID version, Address addr,
656                                 const string& state, const string& event,
657                                 const string& next_state, const string& note)
658{
659  const int EVENT_SPACES = 20;
660  const int ID_SPACES = 3;
661  const int TIME_SPACES = 7;
662  const int COMP_SPACES = 10;
663  const int STATE_SPACES = 6;
664
665  if ((g_debug_ptr->getDebugTime() > 0) &&
666      (g_eventQueue_ptr->getTime() >= g_debug_ptr->getDebugTime())) {
667    (* debug_cout_ptr).flags(ios::right);
668    (* debug_cout_ptr) << setw(TIME_SPACES) << g_eventQueue_ptr->getTime() << " ";
669    (* debug_cout_ptr) << setw(ID_SPACES) << version << " ";
670    (* debug_cout_ptr) << setw(COMP_SPACES) << component;
671    (* debug_cout_ptr) << setw(EVENT_SPACES) << event << " ";
672
673    (* debug_cout_ptr).flags(ios::right);
674    (* debug_cout_ptr) << setw(STATE_SPACES) << state;
675    (* debug_cout_ptr) << ">";
676    (* debug_cout_ptr).flags(ios::left);
677    (* debug_cout_ptr) << setw(STATE_SPACES) << next_state;
678
679    (* debug_cout_ptr) << " " << addr << " " << note;
680
681    (* debug_cout_ptr) << endl;
682  }
683}
684
685// Helper function
686static double process_memory_total()
687{
688  const double MULTIPLIER = 4096.0/(1024.0*1024.0); // 4kB page size, 1024*1024 bytes per MB,
689  ifstream proc_file;
690  proc_file.open("/proc/self/statm");
691  int total_size_in_pages = 0;
692  int res_size_in_pages = 0;
693  proc_file >> total_size_in_pages;
694  proc_file >> res_size_in_pages;
695  return double(total_size_in_pages)*MULTIPLIER; // size in megabytes
696}
697
698static double process_memory_resident()
699{
700  const double MULTIPLIER = 4096.0/(1024.0*1024.0); // 4kB page size, 1024*1024 bytes per MB,
701  ifstream proc_file;
702  proc_file.open("/proc/self/statm");
703  int total_size_in_pages = 0;
704  int res_size_in_pages = 0;
705  proc_file >> total_size_in_pages;
706  proc_file >> res_size_in_pages;
707  return double(res_size_in_pages)*MULTIPLIER; // size in megabytes
708}
709
710void Profiler::rubyWatch(int id){
711    //int rn_g1 = 0;//SIMICS_get_register_number(id, "g1");
712  uint64 tr = 0;//SIMICS_read_register(id, rn_g1);
713    Address watch_address = Address(tr);
714    const int ID_SPACES = 3;
715    const int TIME_SPACES = 7;
716
717    (* debug_cout_ptr).flags(ios::right);
718    (* debug_cout_ptr) << setw(TIME_SPACES) << g_eventQueue_ptr->getTime() << " ";
719    (* debug_cout_ptr) << setw(ID_SPACES) << id << " "
720                       << "RUBY WATCH "
721                       << watch_address
722                       << endl;
723
724    if(!m_watch_address_list_ptr->exist(watch_address)){
725      m_watch_address_list_ptr->add(watch_address, 1);
726    }
727}
728
729bool Profiler::watchAddress(Address addr){
730    if (m_watch_address_list_ptr->exist(addr))
731      return true;
732    else
733      return false;
734}
735
736int64 Profiler::getTotalTransactionsExecuted() const {
737  return m_perProcEndTransaction.sum();
738}
739
740
741Profiler *
742RubyProfilerParams::create()
743{
744    return new Profiler(this);
745}
746