Skip to content

Commit 04c4d91

Browse files
authored
Add option to append to file (LLNL#557)
* Add append option for report services * Add output.append option in report configs
1 parent 45dffec commit 04c4d91

9 files changed

+113
-42
lines changed

include/caliper/common/OutputStream.h

+16-4
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,9 @@ class OutputStream
2424
{
2525
struct OutputStreamImpl;
2626
std::shared_ptr<OutputStreamImpl> mP;
27-
27+
2828
public:
29-
29+
3030
enum StreamType {
3131
None,
3232
StdOut,
@@ -35,9 +35,14 @@ class OutputStream
3535
User
3636
};
3737

38+
enum Mode {
39+
Truncate,
40+
Append
41+
};
42+
3843
/// \brief Returns \a true if the stream is initialized, otherwise
3944
/// returns \a false
40-
operator bool() const;
45+
operator bool() const;
4146

4247
StreamType type() const;
4348

@@ -61,6 +66,13 @@ class OutputStream
6166
void
6267
set_filename(const char* filename);
6368

69+
/// \brief Set stream's output mode for files
70+
///
71+
/// Sets the open mode. \a Truncate resets the file, \a Append
72+
/// appends to it. Only applies to file streams.
73+
void
74+
set_mode(Mode mode);
75+
6476
/// \brief Create stream's filename from the given format string pattern and
6577
/// entry list.
6678
///
@@ -79,5 +91,5 @@ class OutputStream
7991
const CaliperMetadataAccessInterface& db,
8092
const std::vector<Entry>& rec);
8193
};
82-
94+
8395
}

include/caliper/reader/FormatProcessor.h

+2-2
Original file line numberDiff line numberDiff line change
@@ -25,15 +25,15 @@ class FormatProcessor
2525
{
2626
struct FormatProcessorImpl;
2727
std::shared_ptr<FormatProcessorImpl> mP;
28-
28+
2929
public:
3030

3131
/// \brief Create formatter for given query spec and output stream.
3232
FormatProcessor(const QuerySpec&, OutputStream&);
3333

3434
~FormatProcessor();
3535

36-
/// \brief Add snapshot record to formatter.
36+
/// \brief Add snapshot record to formatter.
3737
void process_record(CaliperMetadataAccessInterface&, const EntryList&);
3838

3939
/// \brief Flush formatter contents.

src/caliper/controllers/CudaActivityReportController.cpp

+8-1
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ class CudaActivityReportController : public cali::ChannelController
5959
if (use_mpi) {
6060
config()["CALI_SERVICES_ENABLE" ].append(",mpi,mpireport");
6161
config()["CALI_MPIREPORT_FILENAME"] = opts.get("output", "stderr").to_string();
62+
config()["CALI_MPIREPORT_APPEND" ] = opts.get("output.append").to_string();
6263
config()["CALI_MPIREPORT_WRITE_ON_FINALIZE"] = "false";
6364
config()["CALI_MPIREPORT_LOCAL_CONFIG"] =
6465
opts.build_query("local", {
@@ -74,6 +75,7 @@ class CudaActivityReportController : public cali::ChannelController
7475
} else {
7576
config()["CALI_SERVICES_ENABLE" ].append(",report");
7677
config()["CALI_REPORT_FILENAME" ] = opts.get("output", "stderr").to_string();
78+
config()["CALI_REPORT_APPEND" ] = opts.get("output.append").to_string();
7779
config()["CALI_REPORT_CONFIG" ] =
7880
opts.build_query("local", {
7981
{ "select", serial_select },
@@ -127,7 +129,7 @@ const char* controller_spec = R"json(
127129
"CALI_EVENT_ENABLE_SNAPSHOT_INFO" : "false",
128130
"CALI_CUPTITRACE_SNAPSHOT_DURATION" : "true"
129131
},
130-
"defaults" : { "order_as_visited": "true" },
132+
"defaults" : { "order_as_visited": "true", "output.append": "true" },
131133
"options":
132134
[
133135
{
@@ -139,6 +141,11 @@ const char* controller_spec = R"json(
139141
"name": "show_kernels",
140142
"type": "bool",
141143
"description": "Show kernel names"
144+
},
145+
{
146+
"name": "output.append",
147+
"type": "bool",
148+
"description": "Use append mode when writing to files"
142149
}
143150
]
144151
}

src/caliper/controllers/ROCmActivityReportController.cpp

+8-1
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ class RocmActivityReportController : public cali::ChannelController
5959
if (use_mpi) {
6060
config()["CALI_SERVICES_ENABLE" ].append(",mpi,mpireport");
6161
config()["CALI_MPIREPORT_FILENAME"] = opts.get("output", "stderr").to_string();
62+
config()["CALI_MPIREPORT_APPEND" ] = opts.get("output.append").to_string();
6263
config()["CALI_MPIREPORT_WRITE_ON_FINALIZE"] = "false";
6364
config()["CALI_MPIREPORT_LOCAL_CONFIG"] =
6465
opts.build_query("local", {
@@ -74,6 +75,7 @@ class RocmActivityReportController : public cali::ChannelController
7475
} else {
7576
config()["CALI_SERVICES_ENABLE" ].append(",report");
7677
config()["CALI_REPORT_FILENAME" ] = opts.get("output", "stderr").to_string();
78+
config()["CALI_REPORT_APPEND" ] = opts.get("output.append").to_string();
7779
config()["CALI_REPORT_CONFIG" ] =
7880
opts.build_query("local", {
7981
{ "select", serial_select },
@@ -127,7 +129,7 @@ const char* controller_spec = R"json(
127129
"CALI_EVENT_ENABLE_SNAPSHOT_INFO" : "false",
128130
"CALI_ROCTRACER_TRACE_ACTIVITIES" : "true"
129131
},
130-
"defaults" : { "order_as_visited": "true" },
132+
"defaults" : { "order_as_visited": "true", "output.append": "true" },
131133
"options":
132134
[
133135
{
@@ -140,6 +142,11 @@ const char* controller_spec = R"json(
140142
"type": "bool",
141143
"config": { "CALI_ROCTRACER_RECORD_KERNEL_NAMES": "true" },
142144
"description": "Show kernel names"
145+
},
146+
{
147+
"name": "output.append",
148+
"type": "bool",
149+
"description": "Use append mode when writing to files"
143150
}
144151
]
145152
}

src/caliper/controllers/RuntimeReportController.cpp

+8-1
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@ class RuntimeReportController : public cali::ChannelController
6060
if (use_mpi) {
6161
config()["CALI_SERVICES_ENABLE" ].append(",mpi,mpireport");
6262
config()["CALI_MPIREPORT_FILENAME"] = opts.get("output", "stderr").to_string();
63+
config()["CALI_MPIREPORT_APPEND"] = opts.get("output.append").to_string();
6364
config()["CALI_MPIREPORT_WRITE_ON_FINALIZE"] = "false";
6465
config()["CALI_MPIREPORT_LOCAL_CONFIG"] =
6566
opts.build_query("local", {
@@ -76,6 +77,7 @@ class RuntimeReportController : public cali::ChannelController
7677
} else {
7778
config()["CALI_SERVICES_ENABLE" ].append(",report");
7879
config()["CALI_REPORT_FILENAME" ] = opts.get("output", "stderr").to_string();
80+
config()["CALI_REPORT_APPEND" ] = opts.get("output.append").to_string();
7981
config()["CALI_REPORT_CONFIG" ] =
8082
opts.build_query("local", {
8183
{ "let", local_let },
@@ -131,7 +133,7 @@ const char* runtime_report_spec = R"json(
131133
"CALI_EVENT_ENABLE_SNAPSHOT_INFO" : "false",
132134
"CALI_TIMER_UNIT" : "sec"
133135
},
134-
"defaults" : { "order_as_visited": "true" },
136+
"defaults" : { "order_as_visited": "true", "output.append": "true" },
135137
"options":
136138
[
137139
{
@@ -160,6 +162,11 @@ const char* runtime_report_spec = R"json(
160162
"order by" : [ "sum#sum#sum#time.duration\ desc" ]
161163
}
162164
]
165+
},
166+
{
167+
"name": "output.append",
168+
"type": "bool",
169+
"description": "Use append mode when writing to files"
163170
}
164171
]
165172
};

src/caliper/controllers/SampleReportController.cpp

+8-1
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@ class SampleReportController : public cali::ChannelController
6565
if (use_mpi) {
6666
config()["CALI_SERVICES_ENABLE" ].append(",mpi,mpireport");
6767
config()["CALI_MPIREPORT_FILENAME"] = opts.get("output", "stderr").to_string();
68+
config()["CALI_MPIREPORT_APPEND" ] = opts.get("output.append").to_string();
6869
config()["CALI_MPIREPORT_WRITE_ON_FINALIZE"] = "false";
6970
config()["CALI_MPIREPORT_LOCAL_CONFIG"] =
7071
opts.build_query("local", {
@@ -80,6 +81,7 @@ class SampleReportController : public cali::ChannelController
8081
} else {
8182
config()["CALI_SERVICES_ENABLE" ].append(",report");
8283
config()["CALI_REPORT_FILENAME" ] = opts.get("output", "stderr").to_string();
84+
config()["CALI_REPORT_APPEND" ] = opts.get("output.append").to_string();
8385
config()["CALI_REPORT_CONFIG" ] =
8486
opts.build_query("local", {
8587
{ "select", local_select },
@@ -130,7 +132,7 @@ const char* sample_report_spec = R"json(
130132
"categories" : [ "output", "sampling", "treeformatter", "region" ],
131133
"services" : [ "sampler", "trace" ],
132134
"config" : { "CALI_CHANNEL_FLUSH_ON_EXIT": "false" },
133-
"defaults" : { "source.function": "true" },
135+
"defaults" : { "source.function": "true", "output.append": "true" },
134136
"options":
135137
[
136138
{
@@ -148,6 +150,11 @@ const char* sample_report_spec = R"json(
148150
"name": "aggregate_across_ranks",
149151
"type": "bool",
150152
"description": "Aggregate results across MPI ranks"
153+
},
154+
{
155+
"name": "output.append",
156+
"type": "bool",
157+
"description": "Use append mode when writing to files"
151158
}
152159
]
153160
}

src/common/OutputStream.cpp

+11-3
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99

1010
#include <cstring>
1111
#include <fstream>
12+
#include <iostream>
1213
#include <mutex>
1314
#include <sstream>
1415

@@ -85,6 +86,7 @@ bool check_and_create_directory(const std::string& filepath)
8586
struct OutputStream::OutputStreamImpl
8687
{
8788
StreamType type;
89+
Mode mode;
8890

8991
bool is_initialized;
9092
std::mutex init_mutex;
@@ -109,7 +111,7 @@ struct OutputStream::OutputStreamImpl
109111

110112
if (type == StreamType::File) {
111113
check_and_create_directory(filename);
112-
fs.open(filename);
114+
fs.open(filename, mode == Mode::Append ? std::ios::app : std::ios::trunc);
113115

114116
if (!fs.is_open()) {
115117
type = StreamType::None;
@@ -147,11 +149,11 @@ struct OutputStream::OutputStreamImpl
147149
}
148150

149151
OutputStreamImpl()
150-
: type(StreamType::None), is_initialized(false), user_os(nullptr)
152+
: type(StreamType::None), mode(Truncate), is_initialized(false), user_os(nullptr)
151153
{ }
152154

153155
OutputStreamImpl(const char* name)
154-
: type(StreamType::None), is_initialized(false), filename(name), user_os(nullptr)
156+
: type(StreamType::None), mode(Truncate), is_initialized(false), filename(name), user_os(nullptr)
155157
{ }
156158
};
157159

@@ -181,6 +183,12 @@ OutputStream::stream()
181183
return mP->stream();
182184
}
183185

186+
void
187+
OutputStream::set_mode(OutputStream::Mode mode)
188+
{
189+
mP->mode = mode;
190+
}
191+
184192
void
185193
OutputStream::set_stream(StreamType type)
186194
{

0 commit comments

Comments
 (0)