📄 clock.hh
字号:
#ifndef clock_hh_included#define clock_hh_included#include "assert.hh"#include "checkpoint.hh"#include "inttypes.hh"// A clock value is an UInt64 so that on 32 bit systems it is unlikely that// the clock will wrap arround even if the CPUs are running at a wide range of// frequencies.typedef UInt64 ClockValue;// Two functions used to convert between time units.inline ClockValue nanoseconds(ClockValue cycles, ClockValue freq){ return cycles * 1000 * 1000 * 1000 / freq;}inline ClockValue clock_cycles(ClockValue nanoseconds, ClockValue freq){ return nanoseconds / 1000 / 1000 / 1000 * freq;}// An abstract event class. All user-defined events should be derived from// Event. Event objects are automatically destroyed when they are removed// from the queue. The queue should be polled using the Clock::poll()// function. poll() will process any pending events in order. An event e is// pending if (clock > e.when()), ie. _after_ the registered event time. Note// that events by themselves are not serializable: the user should take care// of that.class Clock;class Event : public virtual Serializable{ friend class Clock;private: static SerialType<Event> type; // The event queue. The queue is circular union { Event *next; Clock *clock; }; // For events that never happen.... static const ClockValue infinity = ~(ClockValue)0;public: // Invocation time ClockValue when; ClockValue interval;private: // Sentinel event constructor. explicit Event(Clock *c) : clock(c), when(infinity) { }public: // Constructors, etc. explicit Event(const SimArgs&) : when(infinity) { assert(UNREACHABLE); } explicit Event(ClockValue w, ClockValue i = 0) : next(0), when(w), interval(i) { } explicit Event(Checkpoint &cp); // Event invocation interface. virtual void invoke(); // Serialization interface. void checkpoint(Checkpoint &cp, bool parent = false) const;};// There is one clock for each CPU installed in the system, maintained by that// CPU. Further, the clock maintains a queue of events that may be polled// using the poll() method.class Clock : public virtual Serializable{private: static SerialType<Clock> type;public: // The current clock value. ClockValue now;private: // Time of the earliest next event. ClockValue earliest; // The event queue. Event *queue;public: // Clock frequency in Hz. const ClockValue freq; // Invoke one or more events from the head of the queue. void invoke_events();public: // A useful value for infinity. static const ClockValue infinity = Event::infinity; // Constructors, etc. explicit Clock(ClockValue f); explicit Clock(const SimArgs&) : freq(0) { assert(UNREACHABLE); } explicit Clock(Checkpoint &cp); ~Clock(); // Access to the clock value by implicit conversion from and to ClockValue. operator ClockValue () const { return now; } Clock& operator=(ClockValue x) { now = x; return *this; } Clock& operator+=(ClockValue x) { now += x; return *this; } Clock& operator-=(ClockValue x) { now -= x; return *this; } Clock& operator++() { ++now; return *this; } Clock& operator--() { --now; return *this; } ClockValue operator++(int) { return now++; } ClockValue operator--(int) { return now--; } // Access to the event queue. void insert(Event *e); // insert into the queue void remove(Event *e); // remove and destroy void poll() // poll the event queue { if (now > earliest) invoke_events(); } // Serialization support. void checkpoint(Checkpoint &cp, bool parent = false) const; // Unit conversions. ClockValue nanoseconds(ClockValue cycles) const { return ::nanoseconds(cycles, freq); } ClockValue cycles(ClockValue nanoseconds) const { return ::clock_cycles(nanoseconds, freq); } ClockValue cycles(ClockValue cycles, ClockValue old_freq) const { return cycles / old_freq * freq; }};#endif // clock_hh_included
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -