Skip to content

Commit d50c322

Browse files
authored
Merge pull request #185 from powerapi-ng/refactor/target-discovery-stat
refactor(target): Reduce cgroup discovery stat overhead
2 parents dc38a36 + fa04367 commit d50c322

3 files changed

Lines changed: 27 additions & 15 deletions

File tree

src/sensor.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ sync_cgroups_running_monitored(struct hwinfo *hwinfo, zhashx_t *container_events
9494
running_targets = zhashx_new();
9595

9696
/* get running (and identifiable) container(s) */
97-
if (target_discover_running(cgroup_basepath, TARGET_TYPE_EVERYTHING, running_targets)) {
97+
if (target_discover_running(cgroup_basepath, running_targets)) {
9898
zsys_error("sensor: error when retrieving the running targets.");
9999
goto out;
100100
}

src/target.c

Lines changed: 25 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -156,34 +156,46 @@ target_destroy(struct target *target)
156156
}
157157

158158
int
159-
target_discover_running(const char *base_path, enum target_type type_mask, zhashx_t *targets)
159+
target_discover_running(const char *base_path, zhashx_t *targets)
160160
{
161161
const char *path[] = { base_path, NULL };
162162
FTS *file_system = NULL;
163163
FTSENT *node = NULL;
164-
enum target_type type;
164+
enum target_type type = TARGET_TYPE_UNKNOWN;
165165
struct target *target = NULL;
166166

167-
file_system = fts_open((char * const *)path, FTS_LOGICAL | FTS_NOCHDIR, NULL);
167+
file_system = fts_open((char * const *) path, FTS_PHYSICAL | FTS_NOCHDIR | FTS_NOSTAT, NULL);
168168
if (!file_system)
169169
return -1;
170170

171171
for (node = fts_read(file_system); node; node = fts_read(file_system)) {
172172
/*
173-
* Filtering the directories having 2 hard links leading to them to only get leaves directories.
174-
* The cgroup subsystems does not support hard links, so this will always work.
173+
* Mark a directory's parent when a child directory is seen in pre-order.
174+
* Then, when the directory is seen in post-order, it is a leaf if no
175+
* child directory marked it.
175176
*/
176-
if (node->fts_info == FTS_D && node->fts_statp->st_nlink == 2) {
177-
type = target_detect_type(node->fts_path);
178-
if ((type & type_mask) && target_validate_type(type, node->fts_path)) {
179-
target = target_create(type, base_path, node->fts_path);
180-
if (target)
181-
zhashx_insert(targets, node->fts_path, target);
182-
}
177+
if (node->fts_info == FTS_D) {
178+
if (node->fts_parent)
179+
node->fts_parent->fts_number = 1;
180+
181+
continue;
183182
}
183+
184+
if (node->fts_info != FTS_DP)
185+
continue;
186+
187+
/* Do not report the traversal root itself as a target */
188+
if (node->fts_level == FTS_ROOTLEVEL)
189+
continue;
190+
191+
/* A child directory was seen, so this directory is not a leaf */
192+
if (node->fts_number != 0)
193+
continue;
194+
195+
target = target_create(type, base_path, node->fts_path);
196+
zhashx_insert(targets, node->fts_path, target);
184197
}
185198

186199
fts_close(file_system);
187200
return 0;
188201
}
189-

src/target.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ void target_destroy(struct target *target);
8888
/*
8989
* target_discover_running returns a list of running targets.
9090
*/
91-
int target_discover_running(const char *base_path, enum target_type type_mask, zhashx_t *targets);
91+
int target_discover_running(const char *base_path, zhashx_t *targets);
9292

9393
#endif /* TARGET_H */
9494

0 commit comments

Comments
 (0)