# A Forwarding Latency Optimization Method for Software Data Plane Based on Spin-Polling
## 5 Design and Implementation
### Feasibility of Packet Forwarding Latency Optimization  
In Section 2.2, we introduced a feature of the spin-polling system, which is always running at full-tilt. Based on this feature, we can infer that the performance of the spin  
Figure 8: Schematic diagram of data plane behavior under high packet arrival rate.  
polling system is not fully utilized until the packet arrival rate reaches the maximum throughput. Therefore, we believe that the wasted performance can be used to optimize the packet forwarding latency. It is straightforward to determine whether the CPU performance utilization reaches the upper limit based on the packet arrival rate. However, additional statistics on the packet arrival rate on the data plane costs a lot and reduce the packet processing capability of the data plane. Since CPU cycles are readily available, cycles that are not spent on valuable tasks can be considered wasted, and cycles that are spent on valuable tasks can be considered valuable. Assuming that the number of cycles of the CPU is constant over a given finite period, then the CPU's performance utilization can be calculated by the following formula:  
\[\eta=\frac{C_{valuable}}{C_{observed}} \tag{7}\]  
where \(C_{valuable}\) is the number of cycles the CPU spends to perform valuable tasks and \(C_{observed}\) is the number of observation cycles. According to Formula 7, when the packet arrival rate reaches the maximum throughput rate of the data plane, the CPU utilization will be close to 100%. Formula (7) can be implemented with a few simple lines of code.  
Algorithm 1 is pseudocode that embeds a CPU performance measurement function in the main loop of the data plane. Our goal is to calculate CPU utilization every time \(T_{observed}\). The while loop that covers _Lines_ 2-14 is the main loop of data plane. In the main loop, _Lines_ 9-13 are the pseudocode for the data plane to process the packets. The data plane receives packets from one Rx queue (_Line_ 9) and then switches to the next Rx queue (_Line_ 10). If no packet arrives (_Line_ 11), the subsequent code will be skipped directly (_Lines_ 11-12). Otherwise, the data plane processes the received packets (_Line_ 13). We use _Lines_ 3-8 and _Line_ 14 to calculate the CPU utilization. The variable _now_ is used to store the current number of CPU cycles, the variable _last_stp_ is used to store the number of cycles when the CPU utilization was last calculated, and the variable _valuable_cycles_ is used to store \(C_{valuable}\). When the time since the last calculation of CPU utilization is greater than \(T_{observed}\) (_Line_ 4), we recalculate the CPU utilization during this period according to Formula (7) (_Line_ 5), and then update the above three variables for the next calculation use (_Lines_ 6-8). The variable _valuable_cycles_ is updated if any packets are processed during a loop (_Line_ 14). Now we can design the latency optimization strategy based on the CPU utilization measured by Formula (7).  
```
1 initializexulable_cycles,last_stp;
2 whileTruedo
3 now\(\leftarrow\) read_stc(); /* ReadthenumberofCPUcyclesatthistime.*/
4 ifnow\(\leftarrow\)last_stp\(>\)T\({}_{observed}\)then
5 utilization=valuable_cycles/(now\(\leftarrow\)last_stp);
6 valuable_cycles\(\leftarrow\)0;
7 now\(\leftarrow\)read_stc();
8 last_stp\(\leftarrow\)now;
9 pkts=xqueue.rx(); /* ReceivepacketsfromRxqueue*/
10 rxqueue=xqueue.next; /* SwitchtothenextRxqueue*/
11 ifpkts.num=0 then /* Ifnopacketsarrive,itgoestoline3*/
12 continue;
13 dataplane.processing(pkts); /* Processpackets*/
14 valuable_cycles+=read_stc0)-now;
```  
**Algorithm 1**CPU Utilization