1/*
2 * Copyright (c) 2012-2014, TU Delft
3 * Copyright (c) 2012-2014, TU Eindhoven
4 * Copyright (c) 2012-2014, TU Kaiserslautern
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions are
9 * met:
10 *
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 *
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
17 *
18 * 3. Neither the name of the copyright holder nor the names of its
19 * contributors may be used to endorse or promote products derived from
20 * this software without specific prior written permission.
21 *
22 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
23 * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
24 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
25 * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
26 * HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
27 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
28 * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
29 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
30 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
31 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
32 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33 *
34 * Authors: Karthik Chandrasekar
35 *          Matthias Jung
36 *          Omar Naji
37 *          Subash Kannoth
38 *          Éder F. Zulian
39 *          Felipe S. Prado
40 *
41 */
42
43#ifndef COMMAND_TIMINGS_H
44#define COMMAND_TIMINGS_H
45
46#include <stdint.h>
47
48#include <vector>
49#include <iostream>
50#include <deque>
51#include <string>
52
53#include "MemCommand.h"
54#include "MemorySpecification.h"
55#include "Utils.h"
56
57namespace Data {
58class CommandAnalysis {
59 public:
60  // Power-Down and Self-refresh related memory states
61  enum memstate {
62    MS_NOT_IN_PD = 0, MS_PDN_F_ACT = 10, MS_PDN_S_ACT = 11, MS_PDN_F_PRE = 12,
63    MS_PDN_S_PRE = 13, MS_SREF = 14
64  };
65
66  // Returns number of reads, writes, acts, pres and refs in the trace
67  CommandAnalysis(const MemorySpecification& memSpec);
68
69  // Number of activate commands per bank
70  std::vector<int64_t> numberofactsBanks;
71  // Number of precharge commands per bank
72  std::vector<int64_t> numberofpresBanks;
73  // Number of reads commands per bank
74  std::vector<int64_t> numberofreadsBanks;
75  // Number of writes commands per bank
76  std::vector<int64_t> numberofwritesBanks;
77  // Number of refresh commands
78  int64_t numberofrefs;
79  // Number of bankwise refresh commands
80  std::vector<int64_t> numberofrefbBanks;
81  // Number of precharge cycles
82  int64_t precycles;
83  // Number of active cycles
84  int64_t actcycles;
85  std::vector<int64_t> actcyclesBanks;
86  // Number of Idle cycles in the active state
87  int64_t idlecycles_act;
88  // Number of Idle cycles in the precharge state
89  int64_t idlecycles_pre;
90  // Number of fast-exit activate power-downs
91  int64_t f_act_pdns;
92  // Number of slow-exit activate power-downs
93  int64_t s_act_pdns;
94  // Number of fast-exit precharged power-downs
95  int64_t f_pre_pdns;
96  // Number of slow-exit activate power-downs
97  int64_t s_pre_pdns;
98  // Number of self-refresh commands
99  int64_t numberofsrefs;
100  // Number of clock cycles in fast-exit activate power-down mode
101  int64_t f_act_pdcycles;
102  // Number of clock cycles in slow-exit activate power-down mode
103  int64_t s_act_pdcycles;
104  // Number of clock cycles in fast-exit precharged power-down mode
105  int64_t f_pre_pdcycles;
106  // Number of clock cycles in slow-exit precharged power-down mode
107  int64_t s_pre_pdcycles;
108  // Number of clock cycles in self-refresh mode (excludes the initial
109  // auto-refresh). During this time the current drawn is IDD6.
110  int64_t sref_cycles;
111  // Number of clock cycles in activate power-up mode
112  int64_t pup_act_cycles;
113  // Number of clock cycles in precharged power-up mode
114  int64_t pup_pre_cycles;
115  // Number of clock cycles in self-refresh power-up mode
116  int64_t spup_cycles;
117
118  // Number of active cycles for the initial auto-refresh when entering
119  // self-refresh mode.
120  int64_t sref_ref_act_cycles;
121  // Number of active auto-refresh cycles in self-refresh mode already used to calculate the energy of the previous windows
122  int64_t sref_ref_act_cycles_window;
123  // Number of precharged auto-refresh cycles in self-refresh mode
124  int64_t sref_ref_pre_cycles;
125  // Number of precharged auto-refresh cycles in self-refresh mode already used to calculate the energy of the previous window
126  int64_t sref_ref_pre_cycles_window;
127  // Number of active auto-refresh cycles during self-refresh exit
128  int64_t spup_ref_act_cycles;
129  // Number of precharged auto-refresh cycles during self-refresh exit
130  int64_t spup_ref_pre_cycles;
131
132  // function for clearing counters
133  void clearStats(const int64_t timestamp);
134
135  // function for clearing arrays
136  void clear();
137
138  // To identify auto-precharges
139  void getCommands(std::vector<MemCommand>&   list,
140                   bool                       lastupdate,
141                   int64_t timestamp = 0);
142
143 private:
144  MemorySpecification memSpec;
145
146  // Possible bank states are precharged or active
147  enum BankState {
148    BANK_PRECHARGED = 0,
149    BANK_ACTIVE
150  };
151
152  int64_t  zero;
153  // Cached last read command from the file
154  std::vector<MemCommand> cached_cmd;
155
156  // Stores the memory commands for analysis
157  std::vector<MemCommand> cmd_list;
158
159  //Stores the memory commands for the next window
160  std::vector<MemCommand> next_window_cmd_list;
161
162  // To save states of the different banks, before entering active
163  // power-down mode (slow/fast-exit).
164  std::vector<BankState> last_bank_state;
165  // Bank state vector
166  std::vector<BankState> bank_state;
167
168  std::vector<int64_t> activation_cycle;
169  // To keep track of the last ACT cycle
170  int64_t latest_act_cycle;
171  // To keep track of the last PRE cycle
172  int64_t latest_pre_cycle;
173  // To keep track of the last READ cycle
174  int64_t latest_read_cycle;
175  // To keep track of the last WRITE cycle
176  int64_t latest_write_cycle;
177
178  // To calculate end of READ operation
179  int64_t end_read_op;
180  // To calculate end of WRITE operation
181  int64_t end_write_op;
182  // To calculate end of ACT operation
183  int64_t end_act_op;
184
185  // Clock cycle when self-refresh was issued
186  int64_t sref_cycle;
187
188  // Latest Self-Refresh clock cycle used to calculate the energy of the previous window
189  int64_t sref_cycle_window;
190
191  // Clock cycle when the latest power-down was issued
192  int64_t pdn_cycle;
193
194  // Memory State
195  unsigned mem_state;
196
197  int64_t num_banks;
198
199  // Clock cycle of first activate command when memory state changes to ACT
200  int64_t first_act_cycle;
201  std::vector<int64_t> first_act_cycle_banks;
202
203  // Clock cycle of last precharge command when memory state changes to PRE
204  int64_t last_pre_cycle;
205
206  // To perform timing analysis of a given set of commands and update command counters
207  void evaluateCommands(std::vector<MemCommand>& cmd_list);
208
209  // Handlers for commands that are getting processed
210  void handleAct(    unsigned bank, int64_t timestamp);
211  void handleRd(     unsigned bank, int64_t timestamp);
212  void handleWr(     unsigned bank, int64_t timestamp);
213  void handleRef(    unsigned bank, int64_t timestamp);
214  void handleRefB(unsigned bank, int64_t timestamp);
215  void handlePre(    unsigned bank, int64_t timestamp);
216  void handlePreA(   unsigned bank, int64_t timestamp);
217  void handlePdnFAct(unsigned bank, int64_t timestamp);
218  void handlePdnSAct(unsigned bank, int64_t timestamp);
219  void handlePdnFPre(unsigned bank, int64_t timestamp);
220  void handlePdnSPre(unsigned bank, int64_t timestamp);
221  void handlePupAct( int64_t timestamp);
222  void handlePupPre( int64_t timestamp);
223  void handleSREn(   unsigned bank, int64_t timestamp);
224  void handleSREx(   unsigned bank, int64_t timestamp);
225  void handleNopEnd( int64_t timestamp);
226
227  // To calculate time of completion of any issued command
228  int64_t timeToCompletion(MemCommand::cmds           type);
229
230  // To update idle period information whenever active cycles may be idle
231  void idle_act_update(int64_t                     latest_read_cycle,
232                       int64_t                     latest_write_cycle,
233                       int64_t                     latest_act_cycle,
234                       int64_t                     timestamp);
235
236  // To update idle period information whenever precharged cycles may be idle
237  void idle_pre_update(int64_t                     timestamp,
238                       int64_t                     latest_pre_cycle);
239
240  // Returns the number of active banks according to the bank_state vector.
241  unsigned get_num_active_banks(void);
242  unsigned nActiveBanks(void);
243
244  bool isPrecharged(unsigned bank);
245
246  void printWarningIfActive(const std::string& warning, int type, int64_t timestamp, unsigned bank);
247  void printWarningIfNotActive(const std::string& warning, int type, int64_t timestamp, unsigned bank);
248  void printWarningIfPoweredDown(const std::string& warning, int type, int64_t timestamp, unsigned bank);
249  void printWarning(const std::string& warning, int type, int64_t timestamp, unsigned bank);
250};
251}
252#endif // ifndef COMMAND_TIMINGS_H
253