|
32 | 32 | #include "sgeobj/sge_qinstance.h" |
33 | 33 | #include "sgeobj/sge_qinstance_state.h" |
34 | 34 | #include "sgeobj/sge_str.h" |
| 35 | +#include "sgeobj/sge_usage.h" |
35 | 36 |
|
36 | 37 | #include "uti/sge_lock.h" |
37 | 38 | #include "uti/sge_log.h" |
@@ -76,6 +77,128 @@ namespace ocs { |
76 | 77 | DRETURN(ret); |
77 | 78 | } |
78 | 79 |
|
| 80 | + /** |
| 81 | + * Emit a single JSONL online_usage record for the running job. |
| 82 | + * |
| 83 | + * When pe_task is non-null the record carries that pe_task's scaled |
| 84 | + * usage and a pe_taskid field. When pe_task is null the record carries |
| 85 | + * the ja_task's scaled usage; if aggregate_pe_tasks is set, scaled |
| 86 | + * usage of every pe_task in `ja_task->JAT_task_list` is summed into the |
| 87 | + * ja_task value (matching the convention used by sge_write_rusage when |
| 88 | + * the PE has `accounting_summary` set). Only the variables listed in |
| 89 | + * `reporting_params=online_usage=...` are emitted under `usage`; |
| 90 | + * variables missing from the scaled usage list are silently skipped |
| 91 | + * (no `null` field). |
| 92 | + * |
| 93 | + * If none of the configured variables have any data yet (e.g. right |
| 94 | + * after job start, before the execd has produced its first usage |
| 95 | + * report), no record is emitted at all — empty-`usage` records would |
| 96 | + * clutter the reporting file without adding information. |
| 97 | + * |
| 98 | + * @see ReportingFileWriter::create_online_usage_records() |
| 99 | + */ |
| 100 | + bool |
| 101 | + JsonReportingFileWriter::create_online_usage_record(lList **answer_list, lListElem *job_report, |
| 102 | + lListElem *job, lListElem *ja_task, lListElem *pe_task, |
| 103 | + bool aggregate_pe_tasks) { |
| 104 | + DENTER(TOP_LAYER); |
| 105 | + |
| 106 | + if (job == nullptr || ja_task == nullptr) { |
| 107 | + DRETURN(true); |
| 108 | + } |
| 109 | + |
| 110 | + // take a snapshot of the configured variable names |
| 111 | + std::vector<std::string> vars; |
| 112 | + sge_mutex_lock(config_mutex_name.c_str(), __func__, __LINE__, &config_mutex); |
| 113 | + vars = online_usage_vars; |
| 114 | + sge_mutex_unlock(config_mutex_name.c_str(), __func__, __LINE__, &config_mutex); |
| 115 | + |
| 116 | + if (vars.empty()) { |
| 117 | + DRETURN(true); |
| 118 | + } |
| 119 | + |
| 120 | + // pe_task != nullptr: report scaled usage of the pe_task, |
| 121 | + // otherwise the (possibly aggregated) scaled usage on the ja_task |
| 122 | + const lList *primary_usage = (pe_task != nullptr) |
| 123 | + ? lGetList(pe_task, PET_scaled_usage) |
| 124 | + : lGetList(ja_task, JAT_scaled_usage_list); |
| 125 | + |
| 126 | + // aggregation only applies to ja_task-level records |
| 127 | + const bool aggregate = (pe_task == nullptr) && aggregate_pe_tasks; |
| 128 | + |
| 129 | + // Suppress empty-usage records: if none of the configured variables |
| 130 | + // have data yet, do not emit anything. |
| 131 | + bool any_data = false; |
| 132 | + for (const auto &name : vars) { |
| 133 | + if (lGetElemStr(primary_usage, UA_name, name.c_str()) != nullptr) { |
| 134 | + any_data = true; |
| 135 | + break; |
| 136 | + } |
| 137 | + if (aggregate) { |
| 138 | + const lListElem *pe_task_iter; |
| 139 | + for_each_ep(pe_task_iter, lGetList(ja_task, JAT_task_list)) { |
| 140 | + if (lGetSubStr(pe_task_iter, UA_name, name.c_str(), PET_scaled_usage) != nullptr) { |
| 141 | + any_data = true; |
| 142 | + break; |
| 143 | + } |
| 144 | + } |
| 145 | + if (any_data) { |
| 146 | + break; |
| 147 | + } |
| 148 | + } |
| 149 | + } |
| 150 | + if (!any_data) { |
| 151 | + DRETURN(true); |
| 152 | + } |
| 153 | + |
| 154 | + rapidjson::StringBuffer stringBuffer; |
| 155 | + rapidjson::Writer<rapidjson::StringBuffer> writer(stringBuffer); |
| 156 | + |
| 157 | + writer.StartObject(); |
| 158 | + write_json(writer, "time", sge_get_gmt64()); |
| 159 | + write_json(writer, "type", "online_usage"); |
| 160 | + write_json(writer, "job_number", lGetUlong(job, JB_job_number)); |
| 161 | + int task_number = job_is_array(job) ? (int) lGetUlong(ja_task, JAT_task_number) : 0; |
| 162 | + write_json(writer, "task_number", task_number); |
| 163 | + if (pe_task != nullptr) { |
| 164 | + write_json(writer, "pe_taskid", lGetString(pe_task, PET_id)); |
| 165 | + } |
| 166 | + |
| 167 | + writer.Key("usage"); |
| 168 | + writer.StartObject(); |
| 169 | + for (const auto &name : vars) { |
| 170 | + double total = 0.0; |
| 171 | + bool found_any = false; |
| 172 | + |
| 173 | + const lListElem *u = lGetElemStr(primary_usage, UA_name, name.c_str()); |
| 174 | + if (u != nullptr) { |
| 175 | + total = lGetDouble(u, UA_value); |
| 176 | + found_any = true; |
| 177 | + } |
| 178 | + |
| 179 | + if (aggregate) { |
| 180 | + const lListElem *pe_task_iter; |
| 181 | + for_each_ep(pe_task_iter, lGetList(ja_task, JAT_task_list)) { |
| 182 | + const lListElem *pu = lGetSubStr(pe_task_iter, UA_name, name.c_str(), PET_scaled_usage); |
| 183 | + if (pu != nullptr) { |
| 184 | + total += lGetDouble(pu, UA_value); |
| 185 | + found_any = true; |
| 186 | + } |
| 187 | + } |
| 188 | + } |
| 189 | + |
| 190 | + if (found_any) { |
| 191 | + write_json(writer, name.c_str(), total); |
| 192 | + } |
| 193 | + } |
| 194 | + writer.EndObject(); |
| 195 | + |
| 196 | + writer.EndObject(); |
| 197 | + create_record(stringBuffer); |
| 198 | + |
| 199 | + DRETURN(true); |
| 200 | + } |
| 201 | + |
79 | 202 | void |
80 | 203 | JsonReportingFileWriter::create_record(rapidjson::StringBuffer &stringBuffer) { |
81 | 204 | stringBuffer.Put('\n'); |
|
0 commit comments