[tarantool-patches] Re: [PATCH v2 2/2] lua: add fiber.top() listing fiber cpu consumption

Serge Petrenko sergepetrenko at tarantool.org
Fri Oct 18 00:39:30 MSK 2019



> 17 окт. 2019 г., в 11:55, Kirill Yukhin <kyukhin at tarantool.org> написал(а):
> 
> Hello,
> 
> On 15 окт 11:02, Georgy Kirichenko wrote:
>> Great job, thanks
>> The only question I have is what about ARM and x86(32), would we like to 
>> support it or we could just disable fiber.top on this architectures using 
>> defines?
>> @kyukhin
> 
> Let's skip this feature for ARM* archs. Just submit follow up issue asking
> for that.

Hi!

I disabled the feature on arm processors.
Please take a look. The diff is below.

diff --git a/src/lib/core/fiber.c b/src/lib/core/fiber.c
index 6973fd678..bea49eb41 100644
--- a/src/lib/core/fiber.c
+++ b/src/lib/core/fiber.c
@@ -37,12 +37,14 @@
 #include <stdlib.h>
 #include <string.h>
 #include <pmatomic.h>
-#include <x86intrin.h> /* __rdtscp() */
-
 #include "assoc.h"
 #include "memory.h"
 #include "trigger.h"
 
+#if ENABLE_FIBER_TOP
+#include <x86intrin.h> /* __rdtscp() */
+#endif /* ENABLE_FIBER_TOP */
+
 #include "third_party/valgrind/memcheck.h"
 
 static int (*fiber_invoke)(fiber_func f, va_list ap);
@@ -83,6 +85,7 @@ static int (*fiber_invoke)(fiber_func f, va_list ap);
 	err;								\
 })
 
+#if ENABLE_FIBER_TOP
 /**
  * An action performed each time a context switch happens.
  * Used to count each fiber's processing time.
@@ -105,6 +108,11 @@ static int (*fiber_invoke)(fiber_func f, va_list ap);
 	cord()->clock_last = clock;					\
 })
 
+
+#else
+#define clock_set_on_csw(caller) ;
+#endif /* ENABLE_FIBER_TOP */
+
 /*
  * Defines a handler to be executed on exit from cord's thread func,
  * accessible via cord()->on_exit (normally NULL). It is used to
@@ -130,8 +138,6 @@ pthread_t main_thread_id;
 static size_t page_size;
 static int stack_direction;
 
-static bool fiber_top_enabled = false;
-
 enum {
 	/* The minimum allowable fiber stack size in bytes */
 	FIBER_STACK_SIZE_MINIMAL = 16384,
@@ -1008,8 +1014,10 @@ fiber_new_ex(const char *name, const struct fiber_attr *fiber_attr,
 	}
 
 	fiber->f = f;
+#if ENABLE_FIBER_TOP
 	fiber->clock_acc = 0;
 	fiber->clock_delta = 0;
+#endif /* ENABLE_FIBER_TOP */
 	/* Excluding reserved range */
 	if (++cord->max_fid < FIBER_ID_MAX_RESERVED)
 		cord->max_fid = FIBER_ID_MAX_RESERVED + 1;
@@ -1074,6 +1082,7 @@ fiber_destroy_all(struct cord *cord)
 						      struct fiber, link));
 }
 
+#if ENABLE_FIBER_TOP
 static void
 loop_on_iteration_start(ev_loop *loop, ev_check *watcher, int revents)
 {
@@ -1141,6 +1150,8 @@ fiber_top_init()
 	ev_check_init(&cord()->check_event, loop_on_iteration_start);
 }
 
+static bool fiber_top_enabled = false;
+
 bool
 fiber_top_is_enabled()
 {
@@ -1170,6 +1181,7 @@ fiber_top_disable()
 		fiber_top_enabled = false;
 	}
 }
+#endif /* ENABLE_FIBER_TOP */
 
 void
 cord_create(struct cord *cord, const char *name)
@@ -1205,12 +1217,13 @@ cord_create(struct cord *cord, const char *name)
 
 	ev_idle_init(&cord->idle_event, fiber_schedule_idle);
 
+#if ENABLE_FIBER_TOP
 	/* fiber.top() currently works only for the main thread. */
 	if (cord_is_main()) {
 		fiber_top_init();
 		fiber_top_enable();
 	}
-
+#endif /* ENABLE_FIBER_TOP */
 	cord_set_name(name);
 
 #if ENABLE_ASAN
diff --git a/src/lib/core/fiber.h b/src/lib/core/fiber.h
index e56a2aa35..d7b5d1f77 100644
--- a/src/lib/core/fiber.h
+++ b/src/lib/core/fiber.h
@@ -46,6 +46,16 @@
 
 #include <third_party/coro/coro.h>
 
+/*
+ * Fiber top doesn't work on ARM processors at the moment,
+ * because we haven't chosen an alternative to rdtsc.
+ */
+#ifdef __CC_ARM
+#define ENABLE_FIBER_TOP 0
+#else
+#define ENABLE_FIBER_TOP 1
+#endif
+
 #if defined(__cplusplus)
 extern "C" {
 #endif /* defined(__cplusplus) */
@@ -386,6 +396,7 @@ struct fiber {
 	uint32_t fid;
 	/** Fiber flags */
 	uint32_t flags;
+#if ENABLE_FIBER_TOP
 	/**
 	 * Accumulated clock value calculated using exponential
 	 * moving average.
@@ -396,6 +407,7 @@ struct fiber {
 	 */
 	uint64_t clock_delta_last;
 	uint64_t clock_delta;
+#endif /* ENABLE_FIBER_TOP */
 	/** Link in cord->alive or cord->dead list. */
 	struct rlist link;
 	/** Link in cord->ready list. */
@@ -467,6 +479,7 @@ struct cord {
 	 * reserved.
 	 */
 	uint32_t max_fid;
+#if ENABLE_FIBER_TOP
 	uint64_t clock_acc;
 	uint64_t clock_delta;
 	uint64_t clock_delta_last;
@@ -474,6 +487,7 @@ struct cord {
 	uint32_t cpu_id_last;
 	uint32_t cpu_miss_count;
 	uint32_t cpu_miss_count_last;
+#endif /* ENABLE_FIBER_TOP */
 	pthread_t id;
 	const struct cord_on_exit *on_exit;
 	/** A helper hash to map id -> fiber. */
@@ -499,6 +513,7 @@ struct cord {
 	 * is no 1 ms delay in case of zero sleep timeout.
 	 */
 	ev_idle idle_event;
+#if ENABLE_FIBER_TOP
 	/** An event triggered on every event loop iteration start. */
 	ev_check check_event;
 	/**
@@ -507,6 +522,7 @@ struct cord {
 	 * time calculations.
 	 */
 	ev_prepare prepare_event;
+#endif /* ENABLE_FIBER_TOP */
 	/** A memory cache for (struct fiber) */
 	struct mempool fiber_mempool;
 	/** A runtime slab cache for general use in this cord. */
@@ -650,6 +666,7 @@ typedef int (*fiber_stat_cb)(struct fiber *f, void *ctx);
 int
 fiber_stat(fiber_stat_cb cb, void *cb_ctx);
 
+#if ENABLE_FIBER_TOP
 bool
 fiber_top_is_enabled();
 
@@ -658,6 +675,7 @@ fiber_top_enable();
 
 void
 fiber_top_disable();
+#endif /* ENABLE_FIBER_TOP */
 
 /** Useful for C unit tests */
 static inline int
diff --git a/src/lua/fiber.c b/src/lua/fiber.c
index d5d9d5573..2adff1536 100644
--- a/src/lua/fiber.c
+++ b/src/lua/fiber.c
@@ -319,6 +319,7 @@ lbox_fiber_statof_nobt(struct fiber *f, void *cb_ctx)
 	return lbox_fiber_statof(f, cb_ctx, false);
 }
 
+#if ENABLE_FIBER_TOP
 static int
 lbox_fiber_top_entry(struct fiber *f, void *cb_ctx)
 {
@@ -371,6 +372,7 @@ lbox_fiber_top_disable(struct lua_State *L)
 	fiber_top_disable();
 	return 0;
 }
+#endif /* ENABLE_FIBER_TOP */
 
 /**
  * Return fiber statistics.
@@ -794,9 +796,11 @@ static const struct luaL_Reg lbox_fiber_meta [] = {
 
 static const struct luaL_Reg fiberlib[] = {
 	{"info", lbox_fiber_info},
+#if ENABLE_FIBER_TOP
 	{"top", lbox_fiber_top},
 	{"top_enable", lbox_fiber_top_enable},
 	{"top_disable", lbox_fiber_top_disable},
+#endif /* ENABLE_FIBER_TOP */
 	{"sleep", lbox_fiber_sleep},
 	{"yield", lbox_fiber_yield},
 	{"self", lbox_fiber_self},

> 
> --
> Regards, Kirill Yukhin





More information about the Tarantool-patches mailing list