Skip to content

Commit

Permalink
have a source-wide initialize called on experiment start
Browse files Browse the repository at this point in the history
  • Loading branch information
Sven Koehler committed Oct 26, 2023
1 parent 5cd7e0c commit 1578db0
Show file tree
Hide file tree
Showing 6 changed files with 36 additions and 10 deletions.
4 changes: 4 additions & 0 deletions src/PowerDataSource.h
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,10 @@ class PowerDataSource
return std::make_pair(sample.timestamp, strlen);
}

// Called once per data source which have open counters, at start of each experiment (after counter creation)
static void initializeExperiment()
{ }

private:
PowerDataSourceDetail *m_detail;
};
13 changes: 13 additions & 0 deletions src/Registry.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#include <algorithm>
#include <map>
#include <utility>
#include <set>

static std::map<std::string,Registry::SourceInfo> s_sources;
static std::map<std::string,std::pair<std::string,std::string>> s_aliases;
Expand Down Expand Up @@ -69,9 +70,21 @@ PowerDataSourcePtr Registry::openCounter(const std::string & name)

PowerDataSourcePtr dataSource = s_sources[sourceName].openCounter(counterName);
dataSource->setName(name);
s_sources[sourceName].has_at_least_one_open_counter = true;

return dataSource;
}

void Registry::callInitializeExperimentsOnOpenSources()
{
for (auto & name_si: s_sources) {
if (name_si.second.has_at_least_one_open_counter) {
name_si.second.has_at_least_one_open_counter = false;
name_si.second.initializeExperiment();
}
}
}

bool Registry::isAvailable(const std::string & sourceName, const std::string & counterName)
{
if (s_sources.find(sourceName) == s_sources.end())
Expand Down
7 changes: 7 additions & 0 deletions src/Registry.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,14 @@ class Registry

// Postpone registration to Registry::setup
std::function<void(SourceInfo & si)> setup;
std::function<void()> initializeExperiment;

bool available() const
{
return !availableCounters.empty();
}

bool has_at_least_one_open_counter;
};

static void setup();
Expand All @@ -30,11 +33,13 @@ class Registry
{
SourceInfo sourceInfo;

sourceInfo.has_at_least_one_open_counter = false;
sourceInfo.setup = [](SourceInfo & si){
si.availableCounters = DataSourceT::detectAvailableCounters();
for (const auto & alias: DataSourceT::possibleAliases()) {
registerAlias(alias.first, DataSourceT::sourceName(), alias.second);
}
si.initializeExperiment = [](){ DataSourceT::initializeExperiment(); };
};

sourceInfo.openCounter = [](const std::string & counterName){
Expand All @@ -48,6 +53,8 @@ class Registry
static std::vector<std::pair<std::string,std::string>> availableAliases();
static PowerDataSourcePtr openCounter(const std::string & name);

static void callInitializeExperimentsOnOpenSources();

private:
static int registerSource(const std::string & sourceName, const SourceInfo & sourceInfo);
static bool isAvailable(const std::string & sourceName, const std::string & counterName);
Expand Down
2 changes: 2 additions & 0 deletions src/Sampler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,8 @@ Sampler::Sampler(std::chrono::milliseconds interval, const std::vector<std::stri
m_detail->worker = std::thread([=]{ run(
settings::continuous_print_flag ? cptick : atick
); });

Registry::callInitializeExperimentsOnOpenSources();
}

Sampler::~Sampler()
Expand Down
18 changes: 8 additions & 10 deletions src/data_sources/AppleM.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -151,17 +151,16 @@ class M1nPointSharedDetail

std::map<std::string, IOReportChannelRef> counter_to_channel;

// I hate this function. It should only be called once per experiment run or for the entire progam
// PinPoint should be adapted to call a source-class-wide "start now, for real" function on registered sources
void update_subscriptions()
void initializeExperiment()
{
CFMutableDictionaryRef _sub_chans;
active_subscription = cf_shared(IOReportCreateSubscription(NULL, desired_channels.get(), &_sub_chans, 0, 0));
if (!active_subscription) {
throw std::runtime_error("Cannot create IOReport subscription");
}
subscribed_channels = cf_shared(_sub_chans);
initialize_experiment();

initial_samples = _pull_raw_samples();
}

void read_and_update_all_open_sources()
Expand Down Expand Up @@ -198,11 +197,6 @@ class M1nPointSharedDetail
}
return res;
}

void initialize_experiment()
{
initial_samples = _pull_raw_samples();
}
};

static M1nPointSharedDetail m1npoint;
Expand Down Expand Up @@ -237,7 +231,6 @@ AppleM::AppleM(const std::string & key) :

m_detail->last_value_key = IOReportChannelGetChannelID(channel);
m1npoint.last_values[m_detail->last_value_key] = AppleMDetail::kOutdated;
m1npoint.update_subscriptions();
}

EnergySample AppleM::read_energy()
Expand Down Expand Up @@ -284,6 +277,11 @@ Aliases AppleM::possibleAliases()
return Aliases();
}

void AppleM::initializeExperiment()
{
m1npoint.initializeExperiment();
}

PINPOINT_REGISTER_DATA_SOURCE(AppleM)

#endif
2 changes: 2 additions & 0 deletions src/data_sources/AppleM.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ class AppleM: public EnergyDataSource

virtual ~AppleM();

static void initializeExperiment();

private:
AppleM(const std::string & key);

Expand Down

0 comments on commit 1578db0

Please sign in to comment.