Skip to contentSkip to conversation

shirq locking rework

RFC PATCH Dmitry Adamushko 2 participants 3 messages Jun 25, 2007 – Jun 30, 2007

lore
Share & tools
lore
#0 lore raw

Hello Jan,

well, take it with 2 huge grains of salt.. it's even not compile-tested :-)

I don't think I've got it really nicer than your first patch.. the only additional point is that 'prev = xnstat_get_current()' reference is also tracked as reference accounting becomes a part of the xnstat interface (not sure we do need it though).

Well (#2), I have to say that the whole intr subsystem looks not that nice (maybe partly due to being overloaded with xnstat details).. so I think, it'd require a closer look.

Anyway, here is what I've got out of your patch so far.. the main issue is whether we go this way or it just makes things even more uglier.

(a side effect : I transformed xnstat_* macroses into 'static inline' function.. I guess, this way it's a bit nicer)

----------

include/nucleus/stat.h +65 -35
diff -upr xenomai-orig/include/nucleus/stat.h xenomai-intr/include/nucleus/stat.h
@@ -31,46 +31,72 @@ typedef struct xnstat_runtime {
 
 	xnticks_t total; /* Accumulated execution time */
 
+	atomic_counter_t refs;
+
 } xnstat_runtime_t;
 
 /* Return current date which can be passed to other xnstat services for
    immediate or lazy accounting. */
-#define xnstat_runtime_now() xnarch_get_cpu_tsc()
+static inline xnticks_t xnstat_runtime_now(void)
+{
+	return xnarch_get_cpu_tsc();
+}
 
 /* Accumulate runtime of the current account until the given date. */
-#define xnstat_runtime_update(sched, start) \
-do { \
-	(sched)->current_account->total += \
-		start - (sched)->last_account_switch; \
-	(sched)->last_account_switch = start; \
-} while (0)
+static inline void xnstat_runtime_update(xnsched_t *sched, xnticks_t start)
+{
+	sched->current_account->total += start - sched->last_account_switch;
+	sched->last_account_switch = start;
+}
 
 /* Update the current account reference, returning the previous one. */
-#define xnstat_runtime_set_current(sched, new_account) \
-({ \
-	xnstat_runtime_t *__prev; \
-	__prev = xnarch_atomic_xchg(&(sched)->current_account, (new_account)); \
-	__prev; \
-})
+static inline void xnstat_runtime_set_current(xnsched_t *sched, xnstat_runtime_t *new_account)
+{
+	xnstat_runtime_t *prev;
+
+	if (likely(new_account))
+		xnarch_atomic_inc(&new_account->refs);
+	prev = xnarch_atomic_xchg(&sched->current_account, new_account);
+	if (likely(prev))
+		xnarch_atomic_dec(&prev->refs);
+}
 
 /* Return the currently active accounting entity. */
-#define xnstat_runtime_get_current(sched) ((sched)->current_account)
+static inline xnstat_runtime_t * xnstat_runtime_get_current(xnsched_t *sched)
+{
+	xnstat_runtime_t *curr = sched->current_account;
+
+	if (likely(curr))
+		xnarch_atomic_inc(&curr->refs);
+}
+
+static inline void xnstat_runtime_put(xnstat_runtime_t *stat)
+{
+	if (likely(stat))
+		xnarch_atomic_dec(&stat->refs);
+}
 
 /* Finalize an account (no need to accumulate the runtime, just mark the
    switch date and set the new account). */
-#define xnstat_runtime_finalize(sched, new_account) \
-do { \
-	(sched)->last_account_switch = xnarch_get_cpu_tsc(); \
-	(sched)->current_account = (new_account); \
-} while (0)
+static inline void xnstat_runtime_finalize(xnsched_t *sched, xnstat_runtime_t *new_account)
+{
+	sched->last_account_switch = xnarch_get_cpu_tsc();
+	xnstat_runtime_set_current(sched, new_account);
+}
 
 /* Reset statistics from inside the accounted entity (e.g. after CPU
    migration). */
-#define xnstat_runtime_reset_stats(stat) \
-do { \
-	(stat)->total = 0; \
-	(stat)->start = xnarch_get_cpu_tsc(); \
-} while (0)
+static inline void xnstat_runtime_reset_stats(xnstat_runtime_t *stat)
+{
+	stat->total = 0;
+	stat->start = xnarch_get_cpu_tsc();
+}
+
+static void xnstat_runtime_sync(xnstat_runtime_t *stat)
+{
+	while (xnarch_atomic_get(&stat->refs))
+		cpu_relax();
+}
 
 
 typedef struct xnstat_counter {
@@ -101,10 +127,12 @@ typedef struct xnstat_runtime {
 
 #define xnstat_runtime_now()					({ 0; })
 #define xnstat_runtime_update(sched, start)			do { } while (0)
-#define xnstat_runtime_set_current(sched, new_account)	({ (void)sched; NULL; })
+#define xnstat_runtime_set_current(sched, new_account)		do { } while (0)
 #define xnstat_runtime_get_current(sched)			({ (void)sched; NULL; })
+#define xnstat_runtime_put(stat)				do { } while (0)
 #define xnstat_runtime_finalize(sched, new_account)		do { } while (0)
 #define xnstat_runtime_reset_stats(account)			do { } while (0)
+#define xnstat_runtime_sync(account)				do { } while (0)
 
 typedef struct xnstat_counter {
 #ifdef __XENO_SIM__
@@ -119,18 +147,20 @@ typedef struct xnstat_counter {
 
 /* Account the runtime of the current account until now, switch to
    new_account, and return the previous one. */
-#define xnstat_runtime_switch(sched, new_account) \
-({ \
-	xnstat_runtime_update(sched, xnstat_runtime_now()); \
-	xnstat_runtime_set_current(sched, new_account); \
-})
+static inline void
+xnstat_runtime_switch(xnsched_t *sched, xnstat_runtime_t *new_account)
+{
+	xnstat_runtime_update(sched, xnstat_runtime_now());
+	xnstat_runtime_set_current(sched, new_account);
+}
 
 /* Account the runtime of the current account until given start time, switch
    to new_account, and return the previous one. */
-#define xnstat_runtime_lazy_switch(sched, new_account, start) \
-({ \
-	xnstat_runtime_update(sched, start); \
-	xnstat_runtime_set_current(sched, new_account); \
-})
+static inline void
+xnstat_runtime_lazy_switch(xnsched_t *sched, xnstat_runtime_t *new_account, xnticks_t start)
+{
+	xnstat_runtime_update(sched, start);
+	xnstat_runtime_set_current(sched, new_account);
+}
 
 #endif /* !_XENO_NUCLEUS_STAT_H */
ksrc/nucleus/intr.c +85 -49

273 lines not on this page · raw message

#1 lore raw

Hi Dmitry,

Quoted 3 lines · Dmitry Adamushko

Hello Jan,

well, take it with 2 huge grains of salt.. it's even not compile-tested :-)

Yeah, that might explain while already trying to parse it manually
failed: What is xnintr_sync_stat_references? :)

Quoted 4 lines

I don't think I've got it really nicer than your first patch.. the only additional point is that 'prev = xnstat_get_current()' reference is also tracked as reference accounting becomes a part of the xnstat interface (not sure we do need it though).

Mind to elaborate on _why_ you think we need this, specifically if it adds new atomic counters? I'm too lazy looking for the probably valid reason on my own. Apropos atomic use counters: already considered all memory barrier issues for your locking scheme?

Quoted 3 lines

Well (#2), I have to say that the whole intr subsystem looks not that nice (maybe partly due to being overloaded with xnstat details).. so I think, it'd require a closer look.

Let's get it right first, then think about aesthetic or even concrete optimisations again.

Quoted 3 lines

Anyway, here is what I've got out of your patch so far.. the main issue is whether we go this way or it just makes things even more uglier.

Again, correctness, race-avoidance worries me first right now.

Quoted 2 lines

(a side effect : I transformed xnstat_* macroses into 'static inline' function.. I guess, this way it's a bit nicer)

Uhh, be careful, I burned my fingers with similar things recently as well. You have to make sure that all types are resolvable for _all_ includers of that header. Otherwise, I'm fine with cleanups like this. But I think there was once a reason for #define.

Thanks,
Jan

PS: Please note my latest refactoring check-in to SVN, touching stat.h.

#2 lore raw

Hello Jan,

I appologize for the huge reply latency.

Quoted 3 lines

Yeah, that might explain while already trying to parse it manually
failed: What is xnintr_sync_stat_references? :)

yeah.. it was supposed to be xnintr_sync_stat_refs()

Quoted 5 lines
Quoted 2 lines

'prev = xnstat_get_current()' reference is also tracked as reference accounting becomes a part of the xnstat interface (not sure we do need it though).

Mind to elaborate on _why_ you think we need this, specifically if it adds new atomic counters?

Forget about it, it was a wrong approach. We do reschedule in xnintr_*_handler() and if 'prev->refs' is non-zero and a newly scheduled thread calls xnstat_runtime_synch() (well, how it could be in theory with this interfcae) before deleting the first thread.. oops. so this 'referencing' scheme is bad anyway.

Note, that if the real re-schedule took place in xnpod_schedule() , we actually don't need to _restore_ 'prev' when we get control back.. it must be already restored by xnpod_schedule() when the preempted thread ('prev' is normally a thread in which context an interrupt occurs) gets CPU back. if I'm not missing something. hum?

...

       if (--sched->inesting == 0 && xnsched_resched_p())
               xnpod_schedule();

(*) <---- 'sched->current_account' should be already == 'prev' in case xnpod_schedule() took place

       xnltt_log_event(xeno_ev_iexit, irq);
       xnstat_runtime_switch(sched, prev);

...

The simpler scheme with xnstat_ accounting would be if we account only time spent in intr->isr() to corresponding intr->stat[cpu].account... This way, all accesses to the later one would be inside xnlock_{get,put}(&xnirqs[irq].lock) sections [*].

It's preciceness (although, it's arguable to some extent) vs. simplicity (e.g. no need for any xnintr_sync_stat_references()). I would still prefer this approach :-)

Otherwise, so far I don't see any much nicer solution that the one illustrated by your first patch.

Quoted 4 lines

Uhh, be careful, I burned my fingers with similar things recently as well. You have to make sure that all types are resolvable for _all_ includers of that header. Otherwise, I'm fine with cleanups like this. But I think there was once a reason for #define.

yeah.. now I recall it as well :-)

Quoted 4 lines

Thanks,
Jan

Signature · 2 lines
Best regards,
Dmitry Adamushko