diff --git a/src/users/log_user_stat.cpp b/src/users/log_user_stat.cpp new file mode 100644 index 0000000000000000000000000000000000000000..fc4ecbc12149ee3f2f33fd55584f3b204e5eaf5e --- /dev/null +++ b/src/users/log_user_stat.cpp @@ -0,0 +1,71 @@ +#include "broker/log_user_stat.hpp" +#include "../pempek_assert.hpp" +BehaviorStat::BehaviorStat(Job* job, + std::string behavior_name,double time_delayed){ + this->job = job; + this->behavior_name = behavior_name; + this->time_delayed = time_delayed; +} + + +std::vector<std::string> BehaviorStat::split_id(){ + std::string job_id = job->id; + std::vector<std::string> splitting; + splitting.push_back(""); + for( std::string::size_type i =0 ; i < job_id.length();i++){ + if(job_id[i]=='!'){ + splitting.push_back(""); + } + else + { + splitting.back() += job_id[i]; + } + } + return splitting; +} +boost::basic_format<char> BehaviorStat::format() +{ + std::vector<std::string> splitting = split_id(); + PPK_ASSERT_ERROR(splitting.size()>=2, + "Error logging the job behavior the job_id should be of the format" + " user_id!job_id"); + return boost::format("%1%,%2%,%3%,%4%,%5%\n") % splitting[0] % + splitting[1] % job->submission_time % behavior_name % time_delayed; +} + +LoggerUserStat::LoggerUserStat(std::string log_folder) +{ + this->log_folder = log_folder; + put_header = true; + write_threshold=4096; + begin_to_write=0; +} + +void LoggerUserStat::add_stat(Job *job,std::string behavior_name, double time_delayed){ + BehaviorStat to_add = BehaviorStat(job,behavior_name,time_delayed); + behaviors.push_back(to_add); + if(behaviors.size()-begin_to_write >= write_threshold){ + log_stat(); + } +} + +void LoggerUserStat::log_stat(){ + std::ofstream file; + if (put_header){ + file.open(log_folder + "/user_stats_behaviors.csv"); + file << "user,job_id,submission_time, behavior_name,time_delayed\n"; + put_header=false; + } + else{ + file.open(log_folder+"/user_stats_behaviors.csv", + std::ofstream::out | std::ofstream::app); + } + + for (std::vector<BehaviorStat>::size_type i =0 ; + i < behaviors.size(); i++){ + BehaviorStat read_behavior = behaviors[i]; + file << read_behavior.format(); + } + behaviors.clear(); + file.close(); +} \ No newline at end of file diff --git a/src/users/log_user_stat.hpp b/src/users/log_user_stat.hpp new file mode 100644 index 0000000000000000000000000000000000000000..31bde846abaea5e1cb8962509ea1d235fa6fe674 --- /dev/null +++ b/src/users/log_user_stat.hpp @@ -0,0 +1,35 @@ +#pragma once + +#include <boost/format.hpp> +#include <fstream> +#include "json_workload.hpp" +/** + * library dedicated to logging user_behavior + */ + +struct BehaviorStat { + Job *job; + std::string behavior_name; + double time_delayed; + double submission_time; + BehaviorStat( Job* job, + std::string behavior_name,double time_delayed); + std::vector<std::string> split_id(); + boost::basic_format<char> format(); +}; + + + + +class LoggerUserStat { +public : + LoggerUserStat(std::string log_folder); + void add_stat(Job *job,std::string behavior_name, double time_delayed); + void log_stat(); +protected : + bool put_header; + std::vector<BehaviorStat>::size_type begin_to_write; + std::vector<BehaviorStat>::size_type write_threshold; + std::string log_folder; + std::vector<BehaviorStat> behaviors; +}; \ No newline at end of file diff --git a/src/users/user_replay.cpp b/src/users/user_replay.cpp index ec23e89803647b2df2d335179e00512880636130..c9d54201025a3a0d039816a4fde6a4774edb0184 100644 --- a/src/users/user_replay.cpp +++ b/src/users/user_replay.cpp @@ -458,7 +458,7 @@ DMUserMultiBehavior::~DMUserMultiBehavior() } void DMUserMultiBehavior::jobs_to_submit( - double date, std::list<shared_ptr<Job>> &jobs, std::list<const Profile *> &profiles) + double date, std::list<Job *> &jobs, std::list<Profile *> &profiles) { DMUserRenonce::jobs_to_submit(date, jobs, profiles); } @@ -558,7 +558,7 @@ bool DMUserDegrad::handle_job(double date, shared_ptr<Job> job, Profile *profile return true; } -bool DMUserRenonce::renonce_job(shared_ptr<Job> job) +bool DMUserRenonce::renonce_job(shared_ptr<Job> job,Profile *profile) { /* Signals that the job must not be executed and delete it from the * original queue */ @@ -570,7 +570,7 @@ bool DMUserRenonce::renonce_job(shared_ptr<Job> job) += job->nb_requested_resources * std::stol(job->profile); return false; } -bool DMUserRenonce::handle_job(double date, shared_ptr<Job> job, Profile *profile) +bool DMUserRenonce::handle_job(double date, Job *job, Profile *profile) { if (dm_window->date_in_dm_window(date)) { @@ -594,17 +594,17 @@ bool DMUserDelay::delay_job(shared_ptr<Job> job) original_trace->remove_job(original_trace->first_job()); job->submission_time = dm_window->sup; - /* Put job back in the queue and sort */ - SortableJobOrder::UpdateInformation update_info(0); - Job *j = new Job(); - *j = *job; - original_trace->insert_job(j); - original_trace->sort_queue(&update_info); + /* Put job back in the queue and sort */ + SortableJobOrder::UpdateInformation update_info(0); + Job *j = new Job(); + *j = *job; + original_trace->insert_job(j); + original_trace->sort_queue(&update_info); /* Return "Do not execute now" */ return false; } -bool DMUserDelay::handle_job(double date, shared_ptr<Job> job, Profile *profile) +bool DMUserDelay::handle_job(double date, Job *job, Profile *profile) { if (dm_window->date_in_dm_window(date)) { @@ -618,7 +618,7 @@ bool DMUserDelay::handle_job(double date, shared_ptr<Job> job, Profile *profile) } } -bool DMUserMultiBehavior::C_you_later_job(double date, double next_time,shared_ptr<Job> job) +bool DMUserMultiBehavior::C_you_later_job(double date, double next_time,Job* job) { /* Log... */ log_behavior(job,"C_you_later",next_time); @@ -630,16 +630,14 @@ bool DMUserMultiBehavior::C_you_later_job(double date, double next_time,shared_p /* Put job back in the queue and sort */ SortableJobOrder::UpdateInformation update_info(0); - Job *j = new Job(); - *j = *job; - original_trace->insert_job(j); + original_trace->insert_job(job); original_trace->sort_queue(&update_info); /* Return "Do not execute now" */ return false; } -bool DMUserMultiBehavior::red_window_behavior(double date,shared_ptr<Job> job,Profile *profile) +bool DMUserMultiBehavior::red_window_behavior(double date,Job* job,Profile *profile) { /* * We decide at random the behavior @@ -688,7 +686,7 @@ bool DMUserMultiBehavior::red_window_behavior(double date,shared_ptr<Job> job,Pr return true; } } -bool DMUserMultiBehavior::yellow_window_behavior(shared_ptr<Job> job,Profile *profile){ +bool DMUserMultiBehavior::yellow_window_behavior(Job* job,Profile *profile){ /* * We decide at random the behavior (rigid, degrad, reconfig) * that will be done on this job @@ -724,7 +722,7 @@ bool DMUserMultiBehavior::yellow_window_behavior(shared_ptr<Job> job,Profile *pr } } -void DMUserMultiBehavior::log_behavior(shared_ptr<Job> job, std::string behavior_name, double delay_time) +void DMUserMultiBehavior::log_behavior(Job *job, std::string behavior_name, double delay_time) { if(logger){ logger->add_stat(job,behavior_name,delay_time); @@ -761,7 +759,7 @@ bool DMUserMultiBehavior::is_in_yellow_window(double date){ } return false; } -bool DMUserMultiBehavior::handle_job(double date, shared_ptr<Job> job, Profile *profile) +bool DMUserMultiBehavior::handle_job(double date, Job *job, Profile *profile) { /* * function called each time the user want to submit a job it does the following : diff --git a/src/users/user_replay.hpp b/src/users/user_replay.hpp index ee07218f80fc2c0c89dfe2da8d3035aca566f15e..92fc44cd11067d5029d7e5f94b748341407f12b9 100644 --- a/src/users/user_replay.hpp +++ b/src/users/user_replay.hpp @@ -4,7 +4,7 @@ #include "users/user.hpp" #include <random> #include "queue.hpp" -#include "users/log_user_stat.hpp" +#include "broker/log_user_stat.hpp" /** * The demand response window during which the users are enclined to reduce * their consumption. (semi-closed interval [inf, sup[) @@ -229,10 +229,10 @@ class DMUserMultiBehavior : public DMUserRenonce, public DMUserDelay, double yellow_prob_total; double next_submission(); void jobs_to_submit( - double date, std::list<shared_ptr<Job>> &jobs, std::list< const Profile *> &profiles); + double date, std::list<Job *> &jobs, std::list<Profile *> &profiles); bool is_in_yellow_window(double date); bool is_in_red_window(double date); - void log_behavior(shared_ptr<Job> job, std::string behavior_name, double delay_time); + void log_behavior(Job* job, std::string behavior_name, double delay_time); protected: DMWindow_list *yellow_windows; DMWindow_list * red_windows; @@ -242,8 +242,8 @@ class DMUserMultiBehavior : public DMUserRenonce, public DMUserDelay, std::uniform_real_distribution<double> distribution = std::uniform_real_distribution<double>(0.0, 1.0); LoggerUserStat *logger = nullptr ; - bool C_you_later_job(double date, double next_time,shared_ptr<Job> job); - bool handle_job(double date,shared_ptr<Job> job,Profile *profile); - bool red_window_behavior(double date,shared_ptr<Job> job, Profile *profile); - bool yellow_window_behavior(shared_ptr<Job>job, Profile *profile); + bool C_you_later_job(double date, double next_time,Job* job); + bool handle_job(double date,Job *job,Profile *profile); + bool red_window_behavior(double date,Job *job, Profile *profile); + bool yellow_window_behavior(Job *job, Profile *profile); }; \ No newline at end of file