Skip to content

Commit daa7713

Browse files
author
Kent Overstreet
committed
bcachefs: bch2_time_stats_init_no_pcpu()
Add a mode to disable automatic switching to percpu mode, useful when a time_stats will only be used by one thread and we don't want to have to flush the percpu buffers. Signed-off-by: Kent Overstreet <[email protected]>
1 parent 7c4cb50 commit daa7713

File tree

2 files changed

+17
-4
lines changed

2 files changed

+17
-4
lines changed

fs/bcachefs/time_stats.c

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,9 @@
1010
#include "eytzinger.h"
1111
#include "time_stats.h"
1212

13+
/* disable automatic switching to percpu mode */
14+
#define TIME_STATS_NONPCPU ((unsigned long) 1)
15+
1316
static const struct time_unit time_units[] = {
1417
{ "ns", 1 },
1518
{ "us", NSEC_PER_USEC },
@@ -123,11 +126,12 @@ void __bch2_time_stats_update(struct bch2_time_stats *stats, u64 start, u64 end)
123126
{
124127
unsigned long flags;
125128

126-
if (!stats->buffer) {
129+
if ((unsigned long) stats->buffer <= TIME_STATS_NONPCPU) {
127130
spin_lock_irqsave(&stats->lock, flags);
128131
time_stats_update_one(stats, start, end);
129132

130-
if (mean_and_variance_weighted_get_mean(stats->freq_stats_weighted, TIME_STATS_MV_WEIGHT) < 32 &&
133+
if (!stats->buffer &&
134+
mean_and_variance_weighted_get_mean(stats->freq_stats_weighted, TIME_STATS_MV_WEIGHT) < 32 &&
131135
stats->duration_stats.n > 1024)
132136
stats->buffer =
133137
alloc_percpu_gfp(struct time_stat_buffer,
@@ -157,7 +161,7 @@ void bch2_time_stats_reset(struct bch2_time_stats *stats)
157161
unsigned offset = offsetof(struct bch2_time_stats, min_duration);
158162
memset((void *) stats + offset, 0, sizeof(*stats) - offset);
159163

160-
if (stats->buffer) {
164+
if ((unsigned long) stats->buffer > TIME_STATS_NONPCPU) {
161165
int cpu;
162166
for_each_possible_cpu(cpu)
163167
per_cpu_ptr(stats->buffer, cpu)->nr = 0;
@@ -167,7 +171,9 @@ void bch2_time_stats_reset(struct bch2_time_stats *stats)
167171

168172
void bch2_time_stats_exit(struct bch2_time_stats *stats)
169173
{
170-
free_percpu(stats->buffer);
174+
if ((unsigned long) stats->buffer > TIME_STATS_NONPCPU)
175+
free_percpu(stats->buffer);
176+
stats->buffer = NULL;
171177
}
172178

173179
void bch2_time_stats_init(struct bch2_time_stats *stats)
@@ -177,3 +183,9 @@ void bch2_time_stats_init(struct bch2_time_stats *stats)
177183
stats->min_freq = U64_MAX;
178184
spin_lock_init(&stats->lock);
179185
}
186+
187+
void bch2_time_stats_init_no_pcpu(struct bch2_time_stats *stats)
188+
{
189+
bch2_time_stats_init(stats);
190+
stats->buffer = (struct time_stat_buffer __percpu *) TIME_STATS_NONPCPU;
191+
}

fs/bcachefs/time_stats.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,7 @@ static inline bool track_event_change(struct bch2_time_stats *stats, bool v)
145145
void bch2_time_stats_reset(struct bch2_time_stats *);
146146
void bch2_time_stats_exit(struct bch2_time_stats *);
147147
void bch2_time_stats_init(struct bch2_time_stats *);
148+
void bch2_time_stats_init_no_pcpu(struct bch2_time_stats *);
148149

149150
static inline void bch2_time_stats_quantiles_exit(struct bch2_time_stats_quantiles *statq)
150151
{

0 commit comments

Comments
 (0)