10.07.2015 Views

Is Parallel Programming Hard, And, If So, What Can You Do About It?

Is Parallel Programming Hard, And, If So, What Can You Do About It?

Is Parallel Programming Hard, And, If So, What Can You Do About It?

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

230 APPENDIX D. READ-COPY UPDATE IMPLEMENTATIONS1 static void2 record_gp_stall_check_time(struct rcu_state *rsp)3 {4 rsp->gp_start = jiffies;5 rsp->jiffies_stall =6 jiffies + RCU_SECONDS_TILL_STALL_CHECK;7 }Figure D.54: record gp stall check time() Codeson for saving the current grace-period number is tocorrectly handle race conditions involving the currentgrace period ending concurrently with the nextinvocation of force_quiescent_state().As noted earlier, lines 53-58 handle the secondand subsequent invocations of force_quiescent_state() in CONFIG_NO_HZ kernels, and all invocationsin non-CONFIG_NO_HZ kernels. Lines 54and 58 invoke rcu_process_dyntick(), which cyclesthrough the CPUs that have still not passedthrough a quiescent state, invoking rcu_implicit_dynticks_qs() on them, which in turn checks to seeif any of these CPUs have passed through dyntickidlestate (if CONFIG_NO_HZ is enabled), checks tosee if we are waiting on any offline CPUs, and finallysends a reschedule IPI to any remaining CPUsnot in the first two groups.1 static void2 check_cpu_stall(struct rcu_state *rsp,3 struct rcu_data *rdp)4 {5 long delta;6 struct rcu_node *rnp;78 delta = jiffies - rsp->jiffies_stall;9 rnp = rdp->mynode;10 if ((rnp->qsmask & rdp->grpmask) && delta >= 0) {11 print_cpu_stall(rsp);12 } else if (rsp->gpnum != rsp->completed &&13 delta >= RCU_STALL_RAT_DELAY) {14 print_other_cpu_stall(rsp);15 }16 }Figure D.55: check cpu stall() Code1 static void print_cpu_stall(struct rcu_state *rsp)2 {3 unsigned long flags;4 struct rcu_node *rnp = rcu_get_root(rsp);56 printk(KERN_ERR7 "INFO: RCU detected CPU %d stall "8 "(t=%lu jiffies)\n",9 smp_processor_id(),10 jiffies - rsp->gp_start);11 dump_stack();12 spin_lock_irqsave(&rnp->lock, flags);13 if ((long)(jiffies - rsp->jiffies_stall) >= 0)14 rsp->jiffies_stall =15 jiffies + RCU_SECONDS_TILL_STALL_RECHECK;16 spin_unlock_irqrestore(&rnp->lock, flags);17 set_need_resched();18 }D.3.9 CPU-Stall DetectionRCU checks for stalled CPUs when the CONFIG_RCU_CPU_STALL_DETECTOR kernel parameter is selected.“Stalled CPUs” are those spinning in thekernel with preemption disabled, which degrades responsetime. These checks are implemented viathe record_gp_stall_check_time(), check_cpu_stall(), print_cpu_stall(), and print_other_cpu_stall() functions, each of which is describedbelow. All of these functions are no-ops when theCONFIG_RCU_CPU_STALL_DETECTOR kernel parameteris not selected.Figure D.54 shows the code for record_gp_stall_check_time(). Line 4 records the currenttime (of the start of the grace period) in jiffies, andlines 5-6 record the time at which CPU stalls shouldbe checked for, should the grace period run on thatlong.FigureD.55showsthecodeforcheck_cpu_stall,which checks to see if the grace period has stretchedon too long, invoking either print_cpu_stall() orprint_other_cpu_stall()inordertoprintaCPUstallwarning message if so.Line 8 computes the number of jiffies since thetime at which stall warnings should be printed,which will be negative if it is not yet time to printFigure D.56: print cpu stall() Codewarnings. Line 9 obtains a pointer to the leafrcu_node structure corresponding to the currentCPU, and line 10 checks to see if the current CPUhas not yet passed through a quiescent state andif the grace period has extended too long (in otherwords, if the current CPU is stalled), with line 11invoking print_cpu_stall() if so.Otherwise, lines 12-13 check to see if the graceperiod is still in effect and if it has extended a coupleof jiffies past the CPU-stall warning duration, withline 14 invoking print_other_cpu_stall() if so.Quick Quiz D.53: Why wait the extra couplejiffies on lines 12-13 in Figure D.55?Figure D.56 shows the code for print_cpu_stall().Line 6-11 prints a console message and dumps thecurrent CPU’s stack, while lines 12-17 compute thetimetothenextCPUstallwarning, shouldthegraceperiod stretch on that much additional time.Quick Quiz D.54: <strong>What</strong> prevents the grace periodfrom ending before the stall warning is printedin Figure D.56?Figure D.57 shows the code for print_other_

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!