Skip to contentSkip to conversation

Issue: Networking performance in Xen VM on Arm64

DISCUSSION Leo Yan 3 participants 16 messages Oct 10, 2022 – Nov 19, 2022

lore
Share & tools
lore

Leo Yan

author
#0 lore raw

Hi there,

I tested the networking performance on my Arm64 platform in Xen virtual machine, below I will try to give out summary for testing result and share some analysis, at the end I want to check a bit from the community and get suggestion before I can proceed.

First of all, if you want to know more details for the profiling, you could access the slides:
https://docs.google.com/presentation/d/1iTQRx8-UYnm19eU6CnVUSaAodKZ0JuRiHYaXBomfu3E/edit?usp=sharing

## Testing summary

The TL;DR is that I used two tools: netperf and ddsperf to test the networking latency and throughput for Xen Dom0 and DomU, the below result shows the performance for sending data from a Xen domain (Dom0 or DomU) to my x86 PC respectively, and performance is poor when transmit data from Xen DomU (Note, I used the default networking bridge configuration when launch Xen VM).

Throughput result:

    Profile     netperf (Mbits/sec)    ddsperf (Mbits/sec)
    Xen-Dom0    939.41                 > 620
    Xen-DomU    107.73                 4~12

Latency result:

    Profile     ddsperf's max ping/pong latency (us)
    Xen-Dom0    200 ~ 1400
    Xen-DomU    > 60,000

## Analysis

The critical thing for the performance is low level network driver if it uses synchronous or asynchronous mode for skb transferring.

When we transfer data from my x86 machine to Xen DomU, the data flow is:

bridge -> xenif (Xen network backend driver) => Dom0
`> xennet driver (Xen net forend driver) => DomU

In this flow, Xen network backend driver (in Dom0) copies skb into the mediate buffer (gnttab_batch_copy()) and notify Xen VM by sending rx irq, the key point is the backend driver doesn't wait for Xen VM to process the skb and directly return to user space, therefore, Xen Dom0 and DomU work in asynchronous mode in this case (Dom0 doesn't need to wait for DomU), the duration for handling a skb is 30+ us.

Conversely, if transmit data from Xen DomU, the flow is:

DomU | Dom0
---------------------------------+------------------------------------

  xennet driver receives skb       |
    `> send tx interrupt to Dom0   |
                                   |  xenif respond tx interrupt

| Copy skb into mediate buffer | Notify DomU (send tx irq)
xennet driver handle tx irq |
free skb |

So we can see when DomU sends out packets, it needs to wait for Dom0 to process the packets, until Dom0 notifies DomU that packet has been processed the net forend driver in DomU releases skb.

This means it's a long way to process skbs: Xen DomU and Dom0 work in synchronous mode, the forend driver in DomU sends out skb and notifies Dom0, Dom0 handles skb and notifies back to DomU, finally DomU knows the skb has been processed and releases it. The duration between sendind and releasing a skb is about 180+ us.

## Questions

Given Xen network driver has been merged in Linux kernel 2.6 (back in 2007), it's very unlikely I am the first person to observe this issue.

I think this is a common issue and not specific to Arm64 arch, the reason is the long latency is mainly caused by Xen networking driver and I did't see the Xen context switching on Arm64 is abnormal (I saw it takes ~10us for context switching between Xen domains).

Could anyone confirm if this is a known issue?

The second question is how to mitigate the long latency when send data from DomU. A possible solution is the Xen network forend driver copies skb into mediate (bounce) buffer, just like what does in Xen net backend driver with gnttab_batch_copy(), in this way the forend driver doesn't need to wait for backend driver response and directly return back. But here I am not clear for the mechanism for Xen grant table, especially if the Xen grant table is only writtable from Dom0, then it would be hard for us to optimize the forend driver in DomU by directly copying skb into the grant table. Any thoughts for this?

Welcome any suggestion and comments. Thanks!

Leo

#1 lore raw

+Xen/Linux maintainers

Quoted 65 lines · Leo Yan

Hi there,

I tested the networking performance on my Arm64 platform in Xen virtual machine, below I will try to give out summary for testing result and share some analysis, at the end I want to check a bit from the community and get suggestion before I can proceed.

First of all, if you want to know more details for the profiling, you could access the slides:
https://docs.google.com/presentation/d/1iTQRx8-UYnm19eU6CnVUSaAodKZ0JuRiHYaXBomfu3E/edit?usp=sharing

## Testing summary

The TL;DR is that I used two tools: netperf and ddsperf to test the networking latency and throughput for Xen Dom0 and DomU, the below result shows the performance for sending data from a Xen domain (Dom0 or DomU) to my x86 PC respectively, and performance is poor when transmit data from Xen DomU (Note, I used the default networking bridge configuration when launch Xen VM).

Throughput result:

    Profile     netperf (Mbits/sec)    ddsperf (Mbits/sec)
    Xen-Dom0    939.41                 > 620
    Xen-DomU    107.73                 4~12

Latency result:

    Profile     ddsperf's max ping/pong latency (us)
    Xen-Dom0    200 ~ 1400
    Xen-DomU    > 60,000

## Analysis

The critical thing for the performance is low level network driver if it uses synchronous or asynchronous mode for skb transferring.

When we transfer data from my x86 machine to Xen DomU, the data flow is:

bridge -> xenif (Xen network backend driver) => Dom0
`> xennet driver (Xen net forend driver) => DomU

In this flow, Xen network backend driver (in Dom0) copies skb into the mediate buffer (gnttab_batch_copy()) and notify Xen VM by sending rx irq, the key point is the backend driver doesn't wait for Xen VM to process the skb and directly return to user space, therefore, Xen Dom0 and DomU work in asynchronous mode in this case (Dom0 doesn't need to wait for DomU), the duration for handling a skb is 30+ us.

Conversely, if transmit data from Xen DomU, the flow is:

DomU | Dom0
---------------------------------+------------------------------------

  xennet driver receives skb       |
    `> send tx interrupt to Dom0   |
                                   |  xenif respond tx interrupt

| Copy skb into mediate buffer | Notify DomU (send tx irq)
xennet driver handle tx irq |
free skb |

So we can see when DomU sends out packets, it needs to wait for Dom0 to process the packets, until Dom0 notifies DomU that packet has been processed the net forend driver in DomU releases skb.

Quoted 5 lines

This means it's a long way to process skbs: Xen DomU and Dom0 work in synchronous mode, the forend driver in DomU sends out skb and notifies Dom0, Dom0 handles skb and notifies back to DomU, finally DomU knows the skb has been processed and releases it. The duration between sendind and releasing a skb is about 180+ us.

180us is not great but above you wrote > 60,000 us. In what ways the two measurements differ?

Quoted 9 lines

## Questions

Given Xen network driver has been merged in Linux kernel 2.6 (back in 2007), it's very unlikely I am the first person to observe this issue.

I think this is a common issue and not specific to Arm64 arch, the reason is the long latency is mainly caused by Xen networking driver and I did't see the Xen context switching on Arm64 is abnormal (I saw it takes ~10us for context switching between Xen domains).

Context switching between domains shouldn't come into the picture. For a latency measurement like that I would make sure to:

- use the null scheduler, sched=null
- use vwfi=native

This way, we can be sure both domains are running and there are no context switches. It should lead to the best latency measurements. Also this is the configuration we use by default at Xilinx.

Quoted 1 line

Could anyone confirm if this is a known issue?

This is not something that was discussed recently as far as I know.

Quoted 6 lines

The second question is how to mitigate the long latency when send data from DomU. A possible solution is the Xen network forend driver copies skb into mediate (bounce) buffer, just like what does in Xen net backend driver with gnttab_batch_copy(), in this way the forend driver doesn't need to wait for backend driver response and directly return back.

About this, I am not super familiar with drivers/net/xen-netfront.c but I take you are referring to xennet_tx_buf_gc? Is that the function that is causing xennet_start_xmit to wait?

I didn't think that waiting for the backend is actually required. I mean, in theory xennet_start_xmit could return without calling xennet_tx_buf_gc, it is just an optimization. But I am not sure about this.

Quoted 4 lines

But here I am not clear for the mechanism for Xen grant table, especially if the Xen grant table is only writtable from Dom0, then it would be hard for us to optimize the forend driver in DomU by directly copying skb into the grant table. Any thoughts for this?

The grant table is a mechanism for the frontend to "grant" access to a specific page to the backend. It is used for dynamic memory sharing between frontend and backend:

- frontend grants a page
- frontend copies grant reference to shared ring
- backend receives request and map granted page
- backend process request
- backend complete request and unmap granted page, send notification
- frontend un-grant the page

This is the regular process. In addition, for performance reason, it is also possible to request Xen to copy a grant to the backend. This is less common, but it is used by PV network, specifically by gnttab_batch_copy. It is an alternative to mapping which is expected to be faster for networking.

author
#2 lore raw

Hi Stefano,

Quoted 1 line · Stefano Stabellini

+Xen/Linux maintainers

Thanks for reviewing.

[...]

Quoted 45 lines
Quoted 45 lines

Throughput result:

    Profile     netperf (Mbits/sec)    ddsperf (Mbits/sec)
    Xen-Dom0    939.41                 > 620
    Xen-DomU    107.73                 4~12

Latency result:

    Profile     ddsperf's max ping/pong latency (us)
    Xen-Dom0    200 ~ 1400
    Xen-DomU    > 60,000

## Analysis

The critical thing for the performance is low level network driver if it uses synchronous or asynchronous mode for skb transferring.

When we transfer data from my x86 machine to Xen DomU, the data flow is:

bridge -> xenif (Xen network backend driver) => Dom0
`> xennet driver (Xen net forend driver) => DomU

In this flow, Xen network backend driver (in Dom0) copies skb into the mediate buffer (gnttab_batch_copy()) and notify Xen VM by sending rx irq, the key point is the backend driver doesn't wait for Xen VM to process the skb and directly return to user space, therefore, Xen Dom0 and DomU work in asynchronous mode in this case (Dom0 doesn't need to wait for DomU), the duration for handling a skb is 30+ us.

Conversely, if transmit data from Xen DomU, the flow is:

DomU | Dom0
---------------------------------+------------------------------------

  xennet driver receives skb       |
    `> send tx interrupt to Dom0   |
                                   |  xenif respond tx interrupt

| Copy skb into mediate buffer | Notify DomU (send tx irq)
xennet driver handle tx irq |
free skb |

So we can see when DomU sends out packets, it needs to wait for Dom0 to process the packets, until Dom0 notifies DomU that packet has been processed the net forend driver in DomU releases skb.

Quoted 8 lines
Quoted 5 lines

This means it's a long way to process skbs: Xen DomU and Dom0 work in synchronous mode, the forend driver in DomU sends out skb and notifies Dom0, Dom0 handles skb and notifies back to DomU, finally DomU knows the skb has been processed and releases it. The duration between sendind and releasing a skb is about 180+ us.

180us is not great but above you wrote > 60,000 us. In what ways the two measurements differ?

108us is measured by using ftrace events 'net_dev_queue' and 'kfree_skb'.

60,000us is the measured maximum latency via ddsperf ping & pong [1], the latency is not only caused by low level driver and hypervisor, it also contains the latency caused by the test program itself.

AFAIK, ddsperf monitors traffic from userspace and throttle data transferring when it detects tx rate is low, I think this is one main reason for the huge latency (60ms+) from ddsperf.

[1] https://cyclonedds.io/docs/cyclonedds/latest/getting_started.html#measuring-latency

Quoted 13 lines
Quoted 9 lines

## Questions

Given Xen network driver has been merged in Linux kernel 2.6 (back in 2007), it's very unlikely I am the first person to observe this issue.

I think this is a common issue and not specific to Arm64 arch, the reason is the long latency is mainly caused by Xen networking driver and I did't see the Xen context switching on Arm64 is abnormal (I saw it takes ~10us for context switching between Xen domains).

Context switching between domains shouldn't come into the picture. For a latency measurement like that I would make sure to:

Quoted 6 lines

- use the null scheduler, sched=null
- use vwfi=native

This way, we can be sure both domains are running and there are no context switches. It should lead to the best latency measurements. Also this is the configuration we use by default at Xilinx.

Okay, will try these two options at my side and share back the testing result.

Quoted 3 lines
Quoted 1 line

Could anyone confirm if this is a known issue?

This is not something that was discussed recently as far as I know.

It is a bit worried me if I am the first one to report this issue.

Either it's because this is a specific issue for Arm64 since Xen is much less deployed on Arm64 than x86 platforms; or I must miss something for causing the poor network performance.

Quoted 10 lines
Quoted 6 lines

The second question is how to mitigate the long latency when send data from DomU. A possible solution is the Xen network forend driver copies skb into mediate (bounce) buffer, just like what does in Xen net backend driver with gnttab_batch_copy(), in this way the forend driver doesn't need to wait for backend driver response and directly return back.

About this, I am not super familiar with drivers/net/xen-netfront.c but I take you are referring to xennet_tx_buf_gc? Is that the function that is causing xennet_start_xmit to wait?

No. We can take the whole flow in xen-netfront.c as:

xennet_start_xmit()
----------> notify Xen Dom0 to process skb <--------- Dom0 copies skb and notify back to DomU
xennet_tx_buf_gc()
softirq/NET_TX : __kfree_skb()

Quoted 4 lines

I didn't think that waiting for the backend is actually required. I mean, in theory xennet_start_xmit could return without calling xennet_tx_buf_gc, it is just an optimization. But I am not sure about this.

The function xennet_start_xmit() will not wait and directly return back, but if we review the whole flow we can see the skb is freed until the softirq NET_TX. In this whole flow, it needs DomU and Dom0 to work together (includes two context switches) to process skb.

Here I mean the optimization is to allow Dom0 and DomU to work in parallel. It could be something like blow, the key point is DomU doesn't need to wait for Dom0's notification.

DomU | Dom0
------------------------------+-------------------------------

  xennet_start_xmit()           |
  copy skb in grant page        |
  notify Xen Dom0               |
                                |  fetch skb from grant page
  xennet_tx_buf_gc()            |  deliver skb to bridge
    kfree_skb()                 |
Quoted 16 lines
Quoted 4 lines

But here I am not clear for the mechanism for Xen grant table, especially if the Xen grant table is only writtable from Dom0, then it would be hard for us to optimize the forend driver in DomU by directly copying skb into the grant table. Any thoughts for this?

The grant table is a mechanism for the frontend to "grant" access to a specific page to the backend. It is used for dynamic memory sharing between frontend and backend:

- frontend grants a page
- frontend copies grant reference to shared ring
- backend receives request and map granted page
- backend process request
- backend complete request and unmap granted page, send notification
- frontend un-grant the page

Quoted 5 lines

This is the regular process. In addition, for performance reason, it is also possible to request Xen to copy a grant to the backend. This is less common, but it is used by PV network, specifically by gnttab_batch_copy. It is an alternative to mapping which is expected to be faster for networking.

Thanks a lot for the shared info.

I saw both net frontend and backend drivers use gnttab_batch_copy(); I need a bit more time to understand ring and grant table and how these two parts co-work together.

Thanks,
Leo

#3 lore raw
Quoted 11 lines · Leo Yan
Quoted 10 lines
Quoted 6 lines

The second question is how to mitigate the long latency when send data from DomU. A possible solution is the Xen network forend driver copies skb into mediate (bounce) buffer, just like what does in Xen net backend driver with gnttab_batch_copy(), in this way the forend driver doesn't need to wait for backend driver response and directly return back.

About this, I am not super familiar with drivers/net/xen-netfront.c but I take you are referring to xennet_tx_buf_gc? Is that the function that is causing xennet_start_xmit to wait?

Quoted 7 lines

No. We can take the whole flow in xen-netfront.c as:

xennet_start_xmit()
----------> notify Xen Dom0 to process skb <--------- Dom0 copies skb and notify back to DomU
xennet_tx_buf_gc()
softirq/NET_TX : __kfree_skb()

Let me premise again that I am not an expert in PV netfront/netback. However, I think the above is only true if DomU and Dom0 are running on the same physical CPU. If you use sched=null as I suggested above, you'll get domU and dom0 running at the same time on different physical CPUs and the workflow doesn't work as described.

It should be:

CPU1: xennet_start_xmit() || CPU2: doing something else
CPU1: notify Xen Dom0 to process skb || CPU2: receive notification
CPU1: return from xennet_start_xmit() || CPU2: Dom0 copies skb
CPU1: do something else || CPU2: notify back to DomU
CPU1: receive irq, xennet_tx_buf_gc() || CPU2: do something else

Quoted 8 lines
Quoted 4 lines

I didn't think that waiting for the backend is actually required. I mean, in theory xennet_start_xmit could return without calling xennet_tx_buf_gc, it is just an optimization. But I am not sure about this.

The function xennet_start_xmit() will not wait and directly return back, but if we review the whole flow we can see the skb is freed until the softirq NET_TX.

Is it an issue that the skb is not freed until later? Is that affecting the latency results? It shouldn't, right? What matters is when dom0 is getting those packets on the physical network interface and that happens before the skb is freed. I am just trying to figure out if we are focusing on the right problem.

Quoted 2 lines

In this whole flow, it needs DomU and Dom0 to work together (includes two context switches) to process skb.

There are not necessarily 2 context switches as things should run in parallel.

Quoted 3 lines

Here I mean the optimization is to allow Dom0 and DomU to work in parallel. It could be something like blow, the key point is DomU doesn't need to wait for Dom0's notification.

I think it is already the case that domU doesn't need to wait for dom0's notification? It is true that domU is waiting for dom0's notification to free the skb but that shouldn't affect latency?

Quoted 8 lines

DomU | Dom0
------------------------------+-------------------------------

  xennet_start_xmit()           |
  copy skb in grant page        |
  notify Xen Dom0               |
                                |  fetch skb from grant page
  xennet_tx_buf_gc()            |  deliver skb to bridge
    kfree_skb()                 |
author
#4 lore raw

Hi Stefano,

Sorry for late response. Please see below comments.

Quoted 26 lines · Stefano Stabellini
Quoted 11 lines · Leo Yan
Quoted 10 lines
Quoted 6 lines

The second question is how to mitigate the long latency when send data from DomU. A possible solution is the Xen network forend driver copies skb into mediate (bounce) buffer, just like what does in Xen net backend driver with gnttab_batch_copy(), in this way the forend driver doesn't need to wait for backend driver response and directly return back.

About this, I am not super familiar with drivers/net/xen-netfront.c but I take you are referring to xennet_tx_buf_gc? Is that the function that is causing xennet_start_xmit to wait?

Quoted 7 lines

No. We can take the whole flow in xen-netfront.c as:

xennet_start_xmit()
----------> notify Xen Dom0 to process skb <--------- Dom0 copies skb and notify back to DomU
xennet_tx_buf_gc()
softirq/NET_TX : __kfree_skb()

Let me premise again that I am not an expert in PV netfront/netback. However, I think the above is only true if DomU and Dom0 are running on the same physical CPU. If you use sched=null as I suggested above, you'll get domU and dom0 running at the same time on different physical CPUs and the workflow doesn't work as described.

Quoted 7 lines

It should be:

CPU1: xennet_start_xmit() || CPU2: doing something else
CPU1: notify Xen Dom0 to process skb || CPU2: receive notification
CPU1: return from xennet_start_xmit() || CPU2: Dom0 copies skb
CPU1: do something else || CPU2: notify back to DomU
CPU1: receive irq, xennet_tx_buf_gc() || CPU2: do something else

Yes, I agree this is ideal case. I tried to set option "sched=null" but I can observe the latency in the second step when CPU1 notify Xen Dom0, Dom0 takes 500us+ to receive the notification.

Please see below detailed log:

DomU log:

4989078512              pub-321   [003]   101.150966: bprint:               xennet_start_xmit: xennet_start_xmit: TSC: 4989078512
4989078573              pub-321   [003]   101.150968: bprint:               xennet_tx_setup_grant: id=24 ref=1816 offset=2 len=1514 TSC: 4989078573
4989078592              pub-321   [003]   101.150969: bprint:               xennet_start_xmit: xennet_notify_tx_irq: TSC: 4989078592

Dom0 log:

4989092169           <idle>-0     [013]   140.121667: bprint:               xenvif_tx_interrupt: xenvif_tx_interrupt: TSC: 4989092169
4989092331           <idle>-0     [013]   140.121673: bprint:               xenvif_tx_build_gops.constprop.0: id=24 ref=1816 offset=2 len=1514 TSC: 4989092331

We can see DomU sends notification with timestamp (raw counter) is 4989078592 and Dom0 receives the interrupt with timestamp 4989092169. Since Dom0 and DomU use the same time counter and the counter frequency is 25MHz, so we can get the delta value (in macroseconds):

    (4989092169 - 4989078592) / 25000000 * 1000 * 1000

= 543us

Which means it takes 543us to let Dom0 to receive the notification. You could see DomU runs in CPU3 and Dom0 runs on CPU13, there should not have contention for CPU resources. Seems to me, it's likely Xen hypervisor takes long time to deliver the interrupt, note, it's not take so long time for every skb transferring, sometimes the time for response a notification is short (about ~10us).

Quoted 11 lines
Quoted 8 lines
Quoted 4 lines

I didn't think that waiting for the backend is actually required. I mean, in theory xennet_start_xmit could return without calling xennet_tx_buf_gc, it is just an optimization. But I am not sure about this.

The function xennet_start_xmit() will not wait and directly return back, but if we review the whole flow we can see the skb is freed until the softirq NET_TX.

Is it an issue that the skb is not freed until later? Is that affecting the latency results? It shouldn't, right?

I did an extra experiment in Xen net forend driver, I enabled the flag "info->bounce = true" so the forend driver will use bounce buffer to store data and release the skb immediately to network core layer.

The throughput can be boosted significantly for this: the netperf result can be improved from 107.73 Mbits/s to 300+ Mbits/s.

Quoted 4 lines

What matters is when dom0 is
getting those packets on the physical network interface and that happens before the skb is freed. I am just trying to figure out if we are focusing on the right problem.

Good point. I agree that releasing skb earlier only can benefit for throughput, but we still cannot resolve the latency issue if Dom0 takes long time to relay packets to phusical network interface.

Quoted 6 lines
Quoted 2 lines

In this whole flow, it needs DomU and Dom0 to work together (includes two context switches) to process skb.

There are not necessarily 2 context switches as things should run in parallel.

Quoted 6 lines
Quoted 3 lines

Here I mean the optimization is to allow Dom0 and DomU to work in parallel. It could be something like blow, the key point is DomU doesn't need to wait for Dom0's notification.

I think it is already the case that domU doesn't need to wait for dom0's notification?

Agree. domU doesn't need to wait for dom0's notification until it uses out the skb can be allocated by the network core layer. This is why I also can tweak core layer's parameters for buffer size (see /proc/sys/net/core/wmem_default and /proc/sys/net/core/wmem_max).

Quoted 2 lines

It is true that domU is waiting for dom0's notification to free the skb but that shouldn't affect latency?

Yeah. I will focus on the elaberated issue above that Dom0 takes long time to receive the notification.

Will keep posted if have any new finding.

Thanks,
Leo

#5 lore raw
Quoted 62 lines · Leo Yan

Hi Stefano,

Sorry for late response. Please see below comments.

Quoted 26 lines · Stefano Stabellini
Quoted 11 lines · Leo Yan
Quoted 10 lines
Quoted 6 lines

The second question is how to mitigate the long latency when send data from DomU. A possible solution is the Xen network forend driver copies skb into mediate (bounce) buffer, just like what does in Xen net backend driver with gnttab_batch_copy(), in this way the forend driver doesn't need to wait for backend driver response and directly return back.

About this, I am not super familiar with drivers/net/xen-netfront.c but I take you are referring to xennet_tx_buf_gc? Is that the function that is causing xennet_start_xmit to wait?

Quoted 7 lines

No. We can take the whole flow in xen-netfront.c as:

xennet_start_xmit()
----------> notify Xen Dom0 to process skb <--------- Dom0 copies skb and notify back to DomU
xennet_tx_buf_gc()
softirq/NET_TX : __kfree_skb()

Let me premise again that I am not an expert in PV netfront/netback. However, I think the above is only true if DomU and Dom0 are running on the same physical CPU. If you use sched=null as I suggested above, you'll get domU and dom0 running at the same time on different physical CPUs and the workflow doesn't work as described.

Quoted 7 lines

It should be:

CPU1: xennet_start_xmit() || CPU2: doing something else
CPU1: notify Xen Dom0 to process skb || CPU2: receive notification
CPU1: return from xennet_start_xmit() || CPU2: Dom0 copies skb
CPU1: do something else || CPU2: notify back to DomU
CPU1: receive irq, xennet_tx_buf_gc() || CPU2: do something else

Yes, I agree this is ideal case. I tried to set option "sched=null" but I can observe the latency in the second step when CPU1 notify Xen Dom0, Dom0 takes 500us+ to receive the notification.

Please see below detailed log:

DomU log:

4989078512              pub-321   [003]   101.150966: bprint:               xennet_start_xmit: xennet_start_xmit: TSC: 4989078512
4989078573              pub-321   [003]   101.150968: bprint:               xennet_tx_setup_grant: id=24 ref=1816 offset=2 len=1514 TSC: 4989078573
4989078592              pub-321   [003]   101.150969: bprint:               xennet_start_xmit: xennet_notify_tx_irq: TSC: 4989078592

Dom0 log:

4989092169           <idle>-0     [013]   140.121667: bprint:               xenvif_tx_interrupt: xenvif_tx_interrupt: TSC: 4989092169
4989092331           <idle>-0     [013]   140.121673: bprint:               xenvif_tx_build_gops.constprop.0: id=24 ref=1816 offset=2 len=1514 TSC: 4989092331

We can see DomU sends notification with timestamp (raw counter) is 4989078592 and Dom0 receives the interrupt with timestamp 4989092169. Since Dom0 and DomU use the same time counter and the counter frequency is 25MHz, so we can get the delta value (in macroseconds):

    (4989092169 - 4989078592) / 25000000 * 1000 * 1000
Quoted 8 lines

= 543us

Which means it takes 543us to let Dom0 to receive the notification. You could see DomU runs in CPU3 and Dom0 runs on CPU13, there should not have contention for CPU resources. Seems to me, it's likely Xen hypervisor takes long time to deliver the interrupt, note, it's not take so long time for every skb transferring, sometimes the time for response a notification is short (about ~10us).

Good find. I think this is worth investigating further. Do you have vwfi=native in your Xen command line as well?

After that, I would add printk also in Xen with the timestamp. The event channel notification code path is the following:

# domU side
xen/arch/arm/vgic-v2.c:vgic_v2_to_sgi
xen/arch/arm/vgic.c:vgic_to_sgi
xen/arch/arm/vgic.c:vgic_inject_irq
xen/arch/arm/vgic.c:vcpu_kick
xen/arch/arm/gic-v2.c:gicv2_send_SGI

# dom0 side
xen/arch/arm/gic.c:do_sgi
xen/arch/arm/traps.c:leave_hypervisor_to_guest

It would be good to understand why sometimes it takes ~10us and some other times it takes ~540us

Quoted 46 lines
Quoted 11 lines
Quoted 8 lines
Quoted 4 lines

I didn't think that waiting for the backend is actually required. I mean, in theory xennet_start_xmit could return without calling xennet_tx_buf_gc, it is just an optimization. But I am not sure about this.

The function xennet_start_xmit() will not wait and directly return back, but if we review the whole flow we can see the skb is freed until the softirq NET_TX.

Is it an issue that the skb is not freed until later? Is that affecting the latency results? It shouldn't, right?

I did an extra experiment in Xen net forend driver, I enabled the flag "info->bounce = true" so the forend driver will use bounce buffer to store data and release the skb immediately to network core layer.

The throughput can be boosted significantly for this: the netperf result can be improved from 107.73 Mbits/s to 300+ Mbits/s.

Quoted 4 lines

What matters is when dom0 is
getting those packets on the physical network interface and that happens before the skb is freed. I am just trying to figure out if we are focusing on the right problem.

Good point. I agree that releasing skb earlier only can benefit for throughput, but we still cannot resolve the latency issue if Dom0 takes long time to relay packets to phusical network interface.

Quoted 6 lines
Quoted 2 lines

In this whole flow, it needs DomU and Dom0 to work together (includes two context switches) to process skb.

There are not necessarily 2 context switches as things should run in parallel.

Quoted 6 lines
Quoted 3 lines

Here I mean the optimization is to allow Dom0 and DomU to work in parallel. It could be something like blow, the key point is DomU doesn't need to wait for Dom0's notification.

I think it is already the case that domU doesn't need to wait for dom0's notification?

Agree. domU doesn't need to wait for dom0's notification until it uses out the skb can be allocated by the network core layer. This is why I also can tweak core layer's parameters for buffer size (see /proc/sys/net/core/wmem_default and /proc/sys/net/core/wmem_max).

Quoted 7 lines
Quoted 2 lines

It is true that domU is waiting for dom0's notification to free the skb but that shouldn't affect latency?

Yeah. I will focus on the elaberated issue above that Dom0 takes long time to receive the notification.

Will keep posted if have any new finding.

Thanks, this is very interesting

author
#6 lore raw

On Mon, Oct 17, 2022 at 04:50:05PM -0700, Stefano Stabellini wrote:

[...]

Quoted 9 lines
Quoted 6 lines

Which means it takes 543us to let Dom0 to receive the notification. You could see DomU runs in CPU3 and Dom0 runs on CPU13, there should not have contention for CPU resources. Seems to me, it's likely Xen hypervisor takes long time to deliver the interrupt, note, it's not take so long time for every skb transferring, sometimes the time for response a notification is short (about ~10us).

Good find. I think this is worth investigating further. Do you have vwfi=native in your Xen command line as well?

Yes, I have added "sched=null" and "vwfi=native" into Xen options:

options=noreboot dom0_mem=4096M bootscrub=0 iommu=on loglvl=error guest_loglvl=error sched=null vwfi=native

Quoted 10 lines

After that, I would add printk also in Xen with the timestamp. The event channel notification code path is the following:

# domU side
xen/arch/arm/vgic-v2.c:vgic_v2_to_sgi
xen/arch/arm/vgic.c:vgic_to_sgi
xen/arch/arm/vgic.c:vgic_inject_irq
xen/arch/arm/vgic.c:vcpu_kick
xen/arch/arm/gic-v2.c:gicv2_send_SGI

Quoted 6 lines
# dom0 side
xen/arch/arm/gic.c:do_sgi
xen/arch/arm/traps.c:leave_hypervisor_to_guest

It would be good to understand why sometimes it takes ~10us and some other times it takes ~540us

Thanks a lot for detailed info.

Just note, in my platform DomU enables GICv3 driver rather than GICv2. This would be a bit different in the Xen code. But it should be easy for me to map to vgic-v3 files.

I have a question for how to trace Xen system. Outputting chars to UART is time costy (usually it's millisecond level), it is not friendly to use console for debugging performance issue. I searched a bit, either "xl dmesg" or xentrace can be used for capturing trace logs, one thing I am not certain is if we can save Xen logs only into log buffer and doesn't output to UART, so that afterwards we can use "xl dmesg" to capture the logs. Could anyone confirm for this is correct usage with "xl dmesg" or I should use xentrace for this case?

Thanks!

Leo

#7 lore raw
Quoted 41 lines · Leo Yan

On Mon, Oct 17, 2022 at 04:50:05PM -0700, Stefano Stabellini wrote:

[...]

Quoted 9 lines
Quoted 6 lines

Which means it takes 543us to let Dom0 to receive the notification. You could see DomU runs in CPU3 and Dom0 runs on CPU13, there should not have contention for CPU resources. Seems to me, it's likely Xen hypervisor takes long time to deliver the interrupt, note, it's not take so long time for every skb transferring, sometimes the time for response a notification is short (about ~10us).

Good find. I think this is worth investigating further. Do you have vwfi=native in your Xen command line as well?

Yes, I have added "sched=null" and "vwfi=native" into Xen options:

options=noreboot dom0_mem=4096M bootscrub=0 iommu=on loglvl=error guest_loglvl=error sched=null vwfi=native

Quoted 10 lines

After that, I would add printk also in Xen with the timestamp. The event channel notification code path is the following:

# domU side
xen/arch/arm/vgic-v2.c:vgic_v2_to_sgi
xen/arch/arm/vgic.c:vgic_to_sgi
xen/arch/arm/vgic.c:vgic_inject_irq
xen/arch/arm/vgic.c:vcpu_kick
xen/arch/arm/gic-v2.c:gicv2_send_SGI

Quoted 6 lines
# dom0 side
xen/arch/arm/gic.c:do_sgi
xen/arch/arm/traps.c:leave_hypervisor_to_guest

It would be good to understand why sometimes it takes ~10us and some other times it takes ~540us

Thanks a lot for detailed info.

Just note, in my platform DomU enables GICv3 driver rather than GICv2. This would be a bit different in the Xen code. But it should be easy for me to map to vgic-v3 files.

Quoted 8 lines

I have a question for how to trace Xen system. Outputting chars to UART is time costy (usually it's millisecond level), it is not friendly to use console for debugging performance issue. I searched a bit, either "xl dmesg" or xentrace can be used for capturing trace logs, one thing I am not certain is if we can save Xen logs only into log buffer and doesn't output to UART, so that afterwards we can use "xl dmesg" to capture the logs. Could anyone confirm for this is correct usage with "xl dmesg" or I should use xentrace for this case?

I am not super familiar about this. But if you take away the console entirely from Xen and give it to dom0 only (no dtuart command line option to Xen), you should get the behavior you described:
- no console output because Xen has no console
- xl dmesg should still work

#8 lore raw

Hi Stefano,

Quoted 51 lines · Stefano Stabellini
Quoted 41 lines · Leo Yan

On Mon, Oct 17, 2022 at 04:50:05PM -0700, Stefano Stabellini wrote:

[...]

Quoted 9 lines
Quoted 6 lines

Which means it takes 543us to let Dom0 to receive the notification. You could see DomU runs in CPU3 and Dom0 runs on CPU13, there should not have contention for CPU resources. Seems to me, it's likely Xen hypervisor takes long time to deliver the interrupt, note, it's not take so long time for every skb transferring, sometimes the time for response a notification is short (about ~10us).

Good find. I think this is worth investigating further. Do you have vwfi=native in your Xen command line as well?

Yes, I have added "sched=null" and "vwfi=native" into Xen options:

options=noreboot dom0_mem=4096M bootscrub=0 iommu=on loglvl=error guest_loglvl=error sched=null vwfi=native

Quoted 10 lines

After that, I would add printk also in Xen with the timestamp. The event channel notification code path is the following:

# domU side
xen/arch/arm/vgic-v2.c:vgic_v2_to_sgi
xen/arch/arm/vgic.c:vgic_to_sgi
xen/arch/arm/vgic.c:vgic_inject_irq
xen/arch/arm/vgic.c:vcpu_kick
xen/arch/arm/gic-v2.c:gicv2_send_SGI

Quoted 6 lines
# dom0 side
xen/arch/arm/gic.c:do_sgi
xen/arch/arm/traps.c:leave_hypervisor_to_guest

It would be good to understand why sometimes it takes ~10us and some other times it takes ~540us

Thanks a lot for detailed info.

Just note, in my platform DomU enables GICv3 driver rather than GICv2. This would be a bit different in the Xen code. But it should be easy for me to map to vgic-v3 files.

Quoted 8 lines

I have a question for how to trace Xen system. Outputting chars to UART is time costy (usually it's millisecond level), it is not friendly to use console for debugging performance issue. I searched a bit, either "xl dmesg" or xentrace can be used for capturing trace logs, one thing I am not certain is if we can save Xen logs only into log buffer and doesn't output to UART, so that afterwards we can use "xl dmesg" to capture the logs. Could anyone confirm for this is correct usage with "xl dmesg" or I should use xentrace for this case?

Quoted 5 lines

I am not super familiar about this. But if you take away the console entirely from Xen and give it to dom0 only (no dtuart command line option to Xen), you should get the behavior you described:
- no console output because Xen has no console
- xl dmesg should still work

You are right, xl dmesg will still work. The console is built using a ring so if you are using "xl dmesg" you will only get the latest log in the ring.

xenconsoled has an option to save the hypervisor log in /var/log/xen/hypervisor.log (default path) if you request to dump them. This is an option at startup and can be set in the init.d/systemd configuration for Xen (see XENCONSOLED_TRACE).

That said, xentrace is better suited if you are looking for low overhead tracing. The console is not directly involved in the process and you can already benefits from the trace point sprinkled in Xen code (such as the scheduling).

Cheers,

Signature · 1 line
Julien Grall
author
#9 lore raw
In reply to Stefano Stabellini

Hi Stefano and all,

On Mon, Oct 17, 2022 at 04:50:05PM -0700, Stefano Stabellini wrote:

[...]

Quoted 28 lines
Quoted 6 lines

We can see DomU sends notification with timestamp (raw counter) is 4989078592 and Dom0 receives the interrupt with timestamp 4989092169. Since Dom0 and DomU use the same time counter and the counter frequency is 25MHz, so we can get the delta value (in macroseconds):

    (4989092169 - 4989078592) / 25000000 * 1000 * 1000
Quoted 8 lines

= 543us

Which means it takes 543us to let Dom0 to receive the notification. You could see DomU runs in CPU3 and Dom0 runs on CPU13, there should not have contention for CPU resources. Seems to me, it's likely Xen hypervisor takes long time to deliver the interrupt, note, it's not take so long time for every skb transferring, sometimes the time for response a notification is short (about ~10us).

Good find. I think this is worth investigating further. Do you have vwfi=native in your Xen command line as well?

After that, I would add printk also in Xen with the timestamp. The event channel notification code path is the following:

# domU side
xen/arch/arm/vgic-v2.c:vgic_v2_to_sgi
xen/arch/arm/vgic.c:vgic_to_sgi
xen/arch/arm/vgic.c:vgic_inject_irq
xen/arch/arm/vgic.c:vcpu_kick
xen/arch/arm/gic-v2.c:gicv2_send_SGI

Quoted 6 lines
# dom0 side
xen/arch/arm/gic.c:do_sgi
xen/arch/arm/traps.c:leave_hypervisor_to_guest

It would be good to understand why sometimes it takes ~10us and some other times it takes ~540us

Some updates for why it takes several hundreds us for Xen backend driver to respond interrupt. The short answer is the vcpu running Xen backend driver needs to switch context, even I have set options "sched=null vwfi=native" in Xen command line.

So please see below detailed logs for how the things happen.

Let's take the timestamp 3842008681 as the start point, it's the time for Xen backend driver sending out notification (xennet_notify_tx_irq); at the timestamp 3842008885 the Xen hypervisor injects the interrupt (it's about ~8us duration from the start point).

And then at the timestamp 3842008935 it invokes vcpu_kick() to kick the virtual CPU for running Xen forend driver, you could see VCPU_PROCESSOR is 11 and VCPU_ID is 9 for dom0, the duration is 10.16us from the start point.

The key point is at this point the vcpu's is_running is 0, this is different from the case without long latency which vcpu's is_running is 1. IIUC, Xen hypervisor needs to take time to restore the vcpu's context, thus we can see the virtual CPU 9 in Dom0 starts to run at the timestamp 3842016505.

3842008548              pub-310   [001]    67.352980: bprint:               xennet_start_xmit: xennet_start_xmit: TSC: 3842008548
3842008652              pub-310   [001]    67.352984: bprint:               xennet_tx_setup_grant: id=52 ref=820 offset=2 len=1514 TSC: 3842008652
3842008681              pub-310   [001]    67.352985: bprint:               xennet_start_xmit: xennet_notify_tx_irq: TSC: 3842008681

3842008689 (XEN) leave_hypervisor_to_guest: CPU_ID: 0 TSC: 3842008689 3842008766 (XEN) EVTCHNOP_send: CPU_ID: 2 TSC: 3842008766
3842008885 (XEN) vgic_inject_irq: CPU_ID: 2 TSC: 3842008885
3842008929 (XEN) leave_hypervisor_to_guest: CPU_ID: 14 TSC: 3842008929 3842008935 (XEN) vcpu_kick: VCPU_PROCESSOR: 11 VCPU_ID: 9 is_running 0 TSC: 3842008935 3842009049 (XEN) leave_hypervisor_to_guest: CPU_ID: 2 TSC: 3842009049

3842009322              pub-310   [001]    67.353011: bprint:               xennet_start_xmit: xennet_start_xmit: TSC: 3842009322
3842009374              pub-310   [001]    67.353013: bprint:               xennet_tx_setup_grant: id=12 ref=780 offset=2050 len=1514 TSC: 3842009374
3842009584              pub-310   [001]    67.353021: bprint:               xennet_start_xmit: xennet_start_xmit: TSC: 3842009584
3842009625 (XEN) leave_hypervisor_to_guest: CPU_ID: 15 TSC: 3842009625
3842009633              pub-310   [001]    67.353023: bprint:               xennet_tx_setup_grant: id=83 ref=851 offset=2 len=1514 TSC: 3842009633
3842009853              pub-310   [001]    67.353032: bprint:               xennet_start_xmit: xennet_start_xmit: TSC: 3842009853
3842009899              pub-310   [001]    67.353034: bprint:               xennet_tx_setup_grant: id=5 ref=773 offset=2050 len=1514 TSC: 3842009899
3842010080              pub-310   [001]    67.353041: bprint:               xennet_start_xmit: xennet_start_xmit: TSC: 3842010080
3842010121              pub-310   [001]    67.353043: bprint:               xennet_tx_setup_grant: id=85 ref=853 offset=2 len=1514 TSC: 3842010121
3842010316              pub-310   [001]    67.353050: bprint:               xennet_start_xmit: xennet_start_xmit: TSC: 3842010316
3842010359              pub-310   [001]    67.353052: bprint:               xennet_tx_setup_grant: id=9 ref=777 offset=2050 len=1514 TSC: 3842010359
3842010553              pub-310   [001]    67.353060: bprint:               xennet_start_xmit: xennet_start_xmit: TSC: 3842010553
3842010599              pub-310   [001]    67.353062: bprint:               xennet_tx_setup_grant: id=35 ref=803 offset=2 len=1514 TSC: 3842010599
3842010792              pub-310   [001]    67.353069: bprint:               xennet_start_xmit: xennet_start_xmit: TSC: 3842010792
3842010838              pub-310   [001]    67.353071: bprint:               xennet_tx_setup_grant: id=17 ref=785 offset=2 len=1514 TSC: 3842010838
3842011032              pub-310   [001]    67.353079: bprint:               xennet_start_xmit: xennet_start_xmit: TSC: 3842011032
3842011072              pub-310   [001]    67.353081: bprint:               xennet_tx_setup_grant: id=18 ref=786 offset=2 len=1514 TSC: 3842011072
3842011124 (XEN) leave_hypervisor_to_guest: CPU_ID: 12 TSC: 3842011124
3842011265              pub-310   [001]    67.353088: bprint:               xennet_start_xmit: xennet_start_xmit: TSC: 3842011265
3842011309              pub-310   [001]    67.353090: bprint:               xennet_tx_setup_grant: id=86 ref=854 offset=3074 len=606 TSC: 3842011309

3842012807 (XEN) leave_hypervisor_to_guest: CPU_ID: 6 TSC: 3842012807 3842013038 (XEN) leave_hypervisor_to_guest: CPU_ID: 10 TSC: 3842013038 3842013568 (XEN) vgic_inject_irq: CPU_ID: 23 TSC: 3842013568
3842013639 (XEN) vcpu_kick: VCPU_PROCESSOR: 23 VCPU_ID: 10 is_running 1 TSC: 3842013639 3842013769 (XEN) leave_hypervisor_to_guest: CPU_ID: 4 TSC: 3842013769 3842013955 (XEN) leave_hypervisor_to_guest: CPU_ID: 8 TSC: 3842013955 3842014327 (XEN) leave_hypervisor_to_guest: CPU_ID: 5 TSC: 3842014327

3842014381              pub-310   [001]    67.353213: bprint:               xennet_start_xmit: xennet_start_xmit: TSC: 3842014381
3842014433              pub-310   [001]    67.353215: bprint:               xennet_tx_setup_grant: id=89 ref=857 offset=2050 len=1514 TSC: 3842014433
3842014621              pub-310   [001]    67.353223: bprint:               xennet_start_xmit: xennet_start_xmit: TSC: 3842014621
3842014663              pub-310   [001]    67.353224: bprint:               xennet_tx_setup_grant: id=38 ref=806 offset=2 len=1514 TSC: 3842014663
3842014852              pub-310   [001]    67.353232: bprint:               xennet_start_xmit: xennet_start_xmit: TSC: 3842014852
3842014909              pub-310   [001]    67.353234: bprint:               xennet_tx_setup_grant: id=4 ref=772 offset=2 len=1422 TSC: 3842014909
3842015153 (XEN) leave_hypervisor_to_guest: CPU_ID: 1 TSC: 3842015153
3842015345 (XEN) leave_hypervisor_to_guest: CPU_ID: 3 TSC: 3842015345
3842015823 (XEN) leave_hypervisor_to_guest: CPU_ID: 13 TSC: 3842015823
3842016505           <idle>-0     [009]    95.098859: bprint:               xenvif_tx_interrupt: xenvif_tx_interrupt: TSC: 3842016505
3842016635           <idle>-0     [009]    95.098863: bprint:               xenvif_tx_build_gops.constprop.0: id=52 ref=820 offset=2 len=1514 TSC: 3842016635
3842016648 (XEN) leave_hypervisor_to_guest: CPU_ID: 9 TSC: 3842016648
3842016671           <idle>-0     [009]    95.098865: bprint:               xenvif_tx_build_gops.constprop.0: id=12 ref=780 offset=2050 len=1514 TSC: 3842016671
3842016700           <idle>-0     [009]    95.098866: bprint:               xenvif_tx_build_gops.constprop.0: id=83 ref=851 offset=2 len=1514 TSC: 3842016700
3842016725           <idle>-0     [009]    95.098867: bprint:               xenvif_tx_build_gops.constprop.0: id=5 ref=773 offset=2050 len=1514 TSC: 3842016725

I did another two experiments.

One experiment is I tried to append option "dom0_max_vcpus=16" into Xen command line, the purpose is I want to allocate 16 virtual CPUs for dom0 and 4 virtual CPUs for domU, and my system has 32 physical CPUs, in theory the physical CPUs are sufficient for dom0 and domU, but I didn't see any performance improvement with option "dom0_max_vcpus=16".

Another experiment is to append option "dom0_vcpus_pin" in Xen command line, this can allow the virtual CPU pins on physical CPU so can avoid context switching. With this option, I can see the throughput is improved from 128.26 Mbits/s to 180.3 Mbits/s. But this result is far from the ideal throughput (800+Mbits/s).

One thing I need to explore is to measure the duration for copying data via grant table (based on the log we can see xennet driver setup grant table and then xenvif driver fetches data from grant table). I will compare the duration for two directions (from dom0 to domU and from domU to dom0). This part might impact the performance heavily.

Thanks,
Leo

#10 lore raw
Quoted 58 lines · Leo Yan

Hi Stefano and all,

On Mon, Oct 17, 2022 at 04:50:05PM -0700, Stefano Stabellini wrote:

[...]

Quoted 28 lines
Quoted 6 lines

We can see DomU sends notification with timestamp (raw counter) is 4989078592 and Dom0 receives the interrupt with timestamp 4989092169. Since Dom0 and DomU use the same time counter and the counter frequency is 25MHz, so we can get the delta value (in macroseconds):

    (4989092169 - 4989078592) / 25000000 * 1000 * 1000
Quoted 8 lines

= 543us

Which means it takes 543us to let Dom0 to receive the notification. You could see DomU runs in CPU3 and Dom0 runs on CPU13, there should not have contention for CPU resources. Seems to me, it's likely Xen hypervisor takes long time to deliver the interrupt, note, it's not take so long time for every skb transferring, sometimes the time for response a notification is short (about ~10us).

Good find. I think this is worth investigating further. Do you have vwfi=native in your Xen command line as well?

After that, I would add printk also in Xen with the timestamp. The event channel notification code path is the following:

# domU side
xen/arch/arm/vgic-v2.c:vgic_v2_to_sgi
xen/arch/arm/vgic.c:vgic_to_sgi
xen/arch/arm/vgic.c:vgic_inject_irq
xen/arch/arm/vgic.c:vcpu_kick
xen/arch/arm/gic-v2.c:gicv2_send_SGI

Quoted 6 lines
# dom0 side
xen/arch/arm/gic.c:do_sgi
xen/arch/arm/traps.c:leave_hypervisor_to_guest

It would be good to understand why sometimes it takes ~10us and some other times it takes ~540us

Some updates for why it takes several hundreds us for Xen backend driver to respond interrupt. The short answer is the vcpu running Xen backend driver needs to switch context, even I have set options "sched=null vwfi=native" in Xen command line.

So please see below detailed logs for how the things happen.

Let's take the timestamp 3842008681 as the start point, it's the time for Xen backend driver sending out notification (xennet_notify_tx_irq); at the timestamp 3842008885 the Xen hypervisor injects the interrupt (it's about ~8us duration from the start point).

And then at the timestamp 3842008935 it invokes vcpu_kick() to kick the virtual CPU for running Xen forend driver, you could see VCPU_PROCESSOR is 11 and VCPU_ID is 9 for dom0, the duration is 10.16us from the start point.

Quoted 5 lines

The key point is at this point the vcpu's is_running is 0, this is different from the case without long latency which vcpu's is_running is 1. IIUC, Xen hypervisor needs to take time to restore the vcpu's context, thus we can see the virtual CPU 9 in Dom0 starts to run at the timestamp 3842016505.

is_running should be always 1 with the NULL scheduler and vwfi=native. That is because VMs are never descheduled. Please double-check.

If you are really running with the NULL scheduler, then I would investigate why the vCPU has is_running == 0 because it should not happen.

Now regarding the results, I can see the timestamp 3842008681 for xennet_notify_tx_irq, 3842008885 for vgic_inject_irq, and 3842008935 for vcpu_kick. Where is the corresponding TSC for the domain receiving the notification?

Also for the other case, starting at 3842016505, can you please highlight the timestamp for vgic_inject_irq, vcpu_kick, and also the one for the domain receiving the notification?

The most interesting timestamps would be the timestamp for vcpu_kick in "notification sending domain" [a], the timestamp for receiving the interrupt in the Xen on pCPU for the "notification receiving domain" [b], and the timestamp for the "notification receiving domain" getting the notification [c].

If really context switch is the issue, then the interesting latency would be between [a] and [b].

Quoted 61 lines
3842008548              pub-310   [001]    67.352980: bprint:               xennet_start_xmit: xennet_start_xmit: TSC: 3842008548
3842008652              pub-310   [001]    67.352984: bprint:               xennet_tx_setup_grant: id=52 ref=820 offset=2 len=1514 TSC: 3842008652
3842008681              pub-310   [001]    67.352985: bprint:               xennet_start_xmit: xennet_notify_tx_irq: TSC: 3842008681

3842008689 (XEN) leave_hypervisor_to_guest: CPU_ID: 0 TSC: 3842008689 3842008766 (XEN) EVTCHNOP_send: CPU_ID: 2 TSC: 3842008766
3842008885 (XEN) vgic_inject_irq: CPU_ID: 2 TSC: 3842008885
3842008929 (XEN) leave_hypervisor_to_guest: CPU_ID: 14 TSC: 3842008929 3842008935 (XEN) vcpu_kick: VCPU_PROCESSOR: 11 VCPU_ID: 9 is_running 0 TSC: 3842008935 3842009049 (XEN) leave_hypervisor_to_guest: CPU_ID: 2 TSC: 3842009049

3842009322              pub-310   [001]    67.353011: bprint:               xennet_start_xmit: xennet_start_xmit: TSC: 3842009322
3842009374              pub-310   [001]    67.353013: bprint:               xennet_tx_setup_grant: id=12 ref=780 offset=2050 len=1514 TSC: 3842009374
3842009584              pub-310   [001]    67.353021: bprint:               xennet_start_xmit: xennet_start_xmit: TSC: 3842009584
3842009625 (XEN) leave_hypervisor_to_guest: CPU_ID: 15 TSC: 3842009625
3842009633              pub-310   [001]    67.353023: bprint:               xennet_tx_setup_grant: id=83 ref=851 offset=2 len=1514 TSC: 3842009633
3842009853              pub-310   [001]    67.353032: bprint:               xennet_start_xmit: xennet_start_xmit: TSC: 3842009853
3842009899              pub-310   [001]    67.353034: bprint:               xennet_tx_setup_grant: id=5 ref=773 offset=2050 len=1514 TSC: 3842009899
3842010080              pub-310   [001]    67.353041: bprint:               xennet_start_xmit: xennet_start_xmit: TSC: 3842010080
3842010121              pub-310   [001]    67.353043: bprint:               xennet_tx_setup_grant: id=85 ref=853 offset=2 len=1514 TSC: 3842010121
3842010316              pub-310   [001]    67.353050: bprint:               xennet_start_xmit: xennet_start_xmit: TSC: 3842010316
3842010359              pub-310   [001]    67.353052: bprint:               xennet_tx_setup_grant: id=9 ref=777 offset=2050 len=1514 TSC: 3842010359
3842010553              pub-310   [001]    67.353060: bprint:               xennet_start_xmit: xennet_start_xmit: TSC: 3842010553
3842010599              pub-310   [001]    67.353062: bprint:               xennet_tx_setup_grant: id=35 ref=803 offset=2 len=1514 TSC: 3842010599
3842010792              pub-310   [001]    67.353069: bprint:               xennet_start_xmit: xennet_start_xmit: TSC: 3842010792
3842010838              pub-310   [001]    67.353071: bprint:               xennet_tx_setup_grant: id=17 ref=785 offset=2 len=1514 TSC: 3842010838
3842011032              pub-310   [001]    67.353079: bprint:               xennet_start_xmit: xennet_start_xmit: TSC: 3842011032
3842011072              pub-310   [001]    67.353081: bprint:               xennet_tx_setup_grant: id=18 ref=786 offset=2 len=1514 TSC: 3842011072
3842011124 (XEN) leave_hypervisor_to_guest: CPU_ID: 12 TSC: 3842011124
3842011265              pub-310   [001]    67.353088: bprint:               xennet_start_xmit: xennet_start_xmit: TSC: 3842011265
3842011309              pub-310   [001]    67.353090: bprint:               xennet_tx_setup_grant: id=86 ref=854 offset=3074 len=606 TSC: 3842011309

3842012807 (XEN) leave_hypervisor_to_guest: CPU_ID: 6 TSC: 3842012807 3842013038 (XEN) leave_hypervisor_to_guest: CPU_ID: 10 TSC: 3842013038 3842013568 (XEN) vgic_inject_irq: CPU_ID: 23 TSC: 3842013568
3842013639 (XEN) vcpu_kick: VCPU_PROCESSOR: 23 VCPU_ID: 10 is_running 1 TSC: 3842013639 3842013769 (XEN) leave_hypervisor_to_guest: CPU_ID: 4 TSC: 3842013769 3842013955 (XEN) leave_hypervisor_to_guest: CPU_ID: 8 TSC: 3842013955 3842014327 (XEN) leave_hypervisor_to_guest: CPU_ID: 5 TSC: 3842014327

3842014381              pub-310   [001]    67.353213: bprint:               xennet_start_xmit: xennet_start_xmit: TSC: 3842014381
3842014433              pub-310   [001]    67.353215: bprint:               xennet_tx_setup_grant: id=89 ref=857 offset=2050 len=1514 TSC: 3842014433
3842014621              pub-310   [001]    67.353223: bprint:               xennet_start_xmit: xennet_start_xmit: TSC: 3842014621
3842014663              pub-310   [001]    67.353224: bprint:               xennet_tx_setup_grant: id=38 ref=806 offset=2 len=1514 TSC: 3842014663
3842014852              pub-310   [001]    67.353232: bprint:               xennet_start_xmit: xennet_start_xmit: TSC: 3842014852
3842014909              pub-310   [001]    67.353234: bprint:               xennet_tx_setup_grant: id=4 ref=772 offset=2 len=1422 TSC: 3842014909
3842015153 (XEN) leave_hypervisor_to_guest: CPU_ID: 1 TSC: 3842015153
3842015345 (XEN) leave_hypervisor_to_guest: CPU_ID: 3 TSC: 3842015345
3842015823 (XEN) leave_hypervisor_to_guest: CPU_ID: 13 TSC: 3842015823
3842016505           <idle>-0     [009]    95.098859: bprint:               xenvif_tx_interrupt: xenvif_tx_interrupt: TSC: 3842016505
3842016635           <idle>-0     [009]    95.098863: bprint:               xenvif_tx_build_gops.constprop.0: id=52 ref=820 offset=2 len=1514 TSC: 3842016635
3842016648 (XEN) leave_hypervisor_to_guest: CPU_ID: 9 TSC: 3842016648
3842016671           <idle>-0     [009]    95.098865: bprint:               xenvif_tx_build_gops.constprop.0: id=12 ref=780 offset=2050 len=1514 TSC: 3842016671
3842016700           <idle>-0     [009]    95.098866: bprint:               xenvif_tx_build_gops.constprop.0: id=83 ref=851 offset=2 len=1514 TSC: 3842016700
3842016725           <idle>-0     [009]    95.098867: bprint:               xenvif_tx_build_gops.constprop.0: id=5 ref=773 offset=2050 len=1514 TSC: 3842016725

I did another two experiments.

One experiment is I tried to append option "dom0_max_vcpus=16" into Xen command line, the purpose is I want to allocate 16 virtual CPUs for dom0 and 4 virtual CPUs for domU, and my system has 32 physical CPUs, in theory the physical CPUs are sufficient for dom0 and domU, but I didn't see any performance improvement with option "dom0_max_vcpus=16".

Quoted 5 lines

Another experiment is to append option "dom0_vcpus_pin" in Xen command line, this can allow the virtual CPU pins on physical CPU so can avoid context switching. With this option, I can see the throughput is improved from 128.26 Mbits/s to 180.3 Mbits/s. But this result is far from the ideal throughput (800+Mbits/s).

Actually this should not happen: like before, dom0_vcpus_pin does nothing with the NULL scheduler and only changes things for credit. Are you really sure you are running the tests with sched=null vwfi=native?

Quoted 5 lines

One thing I need to explore is to measure the duration for copying data via grant table (based on the log we can see xennet driver setup grant table and then xenvif driver fetches data from grant table). I will compare the duration for two directions (from dom0 to domU and from domU to dom0). This part might impact the performance heavily.

author
#11 lore raw

On Fri, Oct 21, 2022 at 02:14:04PM -0700, Stefano Stabellini wrote:

[...]

Quoted 37 lines
Quoted 31 lines
Quoted 7 lines

# domU side
xen/arch/arm/vgic-v2.c:vgic_v2_to_sgi
xen/arch/arm/vgic.c:vgic_to_sgi
xen/arch/arm/vgic.c:vgic_inject_irq
xen/arch/arm/vgic.c:vcpu_kick
xen/arch/arm/gic-v2.c:gicv2_send_SGI

Quoted 6 lines
# dom0 side
xen/arch/arm/gic.c:do_sgi
xen/arch/arm/traps.c:leave_hypervisor_to_guest

It would be good to understand why sometimes it takes ~10us and some other times it takes ~540us

Some updates for why it takes several hundreds us for Xen backend driver to respond interrupt. The short answer is the vcpu running Xen backend driver needs to switch context, even I have set options "sched=null vwfi=native" in Xen command line.

So please see below detailed logs for how the things happen.

Let's take the timestamp 3842008681 as the start point, it's the time for Xen backend driver sending out notification (xennet_notify_tx_irq); at the timestamp 3842008885 the Xen hypervisor injects the interrupt (it's about ~8us duration from the start point).

And then at the timestamp 3842008935 it invokes vcpu_kick() to kick the virtual CPU for running Xen forend driver, you could see VCPU_PROCESSOR is 11 and VCPU_ID is 9 for dom0, the duration is 10.16us from the start point.

Quoted 5 lines

The key point is at this point the vcpu's is_running is 0, this is different from the case without long latency which vcpu's is_running is 1. IIUC, Xen hypervisor needs to take time to restore the vcpu's context, thus we can see the virtual CPU 9 in Dom0 starts to run at the timestamp 3842016505.

Quoted 6 lines

is_running should be always 1 with the NULL scheduler and vwfi=native. That is because VMs are never descheduled. Please double-check.

If you are really running with the NULL scheduler, then I would investigate why the vCPU has is_running == 0 because it should not happen.

Correct for this: it's my bad that I didn't really enable NULL scheduler in my code base. After I enabled NULL scheduler, the latency by context switching is dismissed.

 8963              pub-338   [002]   217.777652: bprint:               xennet_tx_setup_grant: id=60 ref=1340 offset=2 len=1514 TSC: 7892178799
 8964              pub-338   [002]   217.777662: bprint:               xennet_tx_setup_grant: id=82 ref=1362 offset=2050 len=1006 TSC: 7892179043
 8965     ksoftirqd/12-75    [012]   255.466914: bprint:               xenvif_tx_build_gops.constprop.0: id=60 ref=1340 offset=2 len=1514 TSC: 7892179731
 8966     ksoftirqd/12-75    [012]   255.466915: bprint:               xenvif_tx_build_gops.constprop.0: id=82 ref=1362 offset=2050 len=1006 TSC: 7892179761
 8967              pub-338   [002]   217.778057: bprint:               xennet_tx_setup_grant: id=60 ref=1340 offset=2050 len=1514 TSC: 7892188930
 8968              pub-338   [002]   217.778072: bprint:               xennet_tx_setup_grant: id=53 ref=1333 offset=2 len=1514 TSC: 7892189293
 8969       containerd-2965  [012]   255.467304: bprint:               xenvif_tx_build_gops.constprop.0: id=60 ref=1340 offset=2050 len=1514 TSC: 7892189479
 8970       containerd-2965  [012]   255.467306: bprint:               xenvif_tx_build_gops.constprop.0: id=53 ref=1333 offset=2 len=1514 TSC: 7892189533

So the xennet (Xen net forend driver) and xenvif (net backend driver) work in parallel. Please note, I didn't see networking performance improvement after changed to use NULL scheduler.

Now I will compare the duration for two directions, one direction is sending data from xennet to xenvif, and another is the reversed direction. It's very likely the two directions have significant difference for sending data with grant tables, you could see in above log, it takes 20~30us to transmit a data block (we can use the id number and grant table's ref number to match the data block in xennet driver and xenvif driver).

Quoted 9 lines

Now regarding the results, I can see the timestamp 3842008681 for xennet_notify_tx_irq, 3842008885 for vgic_inject_irq, and 3842008935 for vcpu_kick. Where is the corresponding TSC for the domain receiving the notification?

Also for the other case, starting at 3842016505, can you please highlight the timestamp for vgic_inject_irq, vcpu_kick, and also the one for the domain receiving the notification?

Quoted 8 lines

The most interesting timestamps would be the timestamp for vcpu_kick in "notification sending domain" [a], the timestamp for receiving the interrupt in the Xen on pCPU for the "notification receiving domain" [b], and the timestamp for the "notification receiving domain" getting the notification [c].

If really context switch is the issue, then the interesting latency would be between [a] and [b].

Understand. I agree that I didn't move into more details, the main reason is Xen dmesg buffer is fragile after adding more logs, e.g. after I added log in the function gicv3_send_sgi(), Xen will stuck during the booting phase, and after adding logs in leave_hypervisor_to_guest() it will introduce huge logs (so I need to only trace for first 16 CPUs to mitigate log flood).

I think it would be better to enable xentrace for my profiling at my side. If I have any further data, will share back.

[...]

Quoted 9 lines
Quoted 5 lines

Another experiment is to append option "dom0_vcpus_pin" in Xen command line, this can allow the virtual CPU pins on physical CPU so can avoid context switching. With this option, I can see the throughput is improved from 128.26 Mbits/s to 180.3 Mbits/s. But this result is far from the ideal throughput (800+Mbits/s).

Actually this should not happen: like before, dom0_vcpus_pin does nothing with the NULL scheduler and only changes things for credit. Are you really sure you are running the tests with sched=null vwfi=native?

You are right. After I changed to use NULL scheduler, dom0_vcpus_pin doesn't improve the performance, sorry for noise and please ignore it.

Thanks,
Leo

#12 lore raw
Quoted 8 lines · Leo Yan
Quoted 3 lines

If you are really running with the NULL scheduler, then I would investigate why the vCPU has is_running == 0 because it should not happen.

Correct for this: it's my bad that I didn't really enable NULL scheduler in my code base. After I enabled NULL scheduler, the latency by context switching is dismissed.

Quoted 8 lines
 8963              pub-338   [002]   217.777652: bprint:               xennet_tx_setup_grant: id=60 ref=1340 offset=2 len=1514 TSC: 7892178799
 8964              pub-338   [002]   217.777662: bprint:               xennet_tx_setup_grant: id=82 ref=1362 offset=2050 len=1006 TSC: 7892179043
 8965     ksoftirqd/12-75    [012]   255.466914: bprint:               xenvif_tx_build_gops.constprop.0: id=60 ref=1340 offset=2 len=1514 TSC: 7892179731
 8966     ksoftirqd/12-75    [012]   255.466915: bprint:               xenvif_tx_build_gops.constprop.0: id=82 ref=1362 offset=2050 len=1006 TSC: 7892179761
 8967              pub-338   [002]   217.778057: bprint:               xennet_tx_setup_grant: id=60 ref=1340 offset=2050 len=1514 TSC: 7892188930
 8968              pub-338   [002]   217.778072: bprint:               xennet_tx_setup_grant: id=53 ref=1333 offset=2 len=1514 TSC: 7892189293
 8969       containerd-2965  [012]   255.467304: bprint:               xenvif_tx_build_gops.constprop.0: id=60 ref=1340 offset=2050 len=1514 TSC: 7892189479
 8970       containerd-2965  [012]   255.467306: bprint:               xenvif_tx_build_gops.constprop.0: id=53 ref=1333 offset=2 len=1514 TSC: 7892189533

I am having difficulty following the messages. Are the two points [a] and [b] as described in the previous email shown here?

Quoted 37 lines

So the xennet (Xen net forend driver) and xenvif (net backend driver) work in parallel. Please note, I didn't see networking performance improvement after changed to use NULL scheduler.

Now I will compare the duration for two directions, one direction is sending data from xennet to xenvif, and another is the reversed direction. It's very likely the two directions have significant difference for sending data with grant tables, you could see in above log, it takes 20~30us to transmit a data block (we can use the id number and grant table's ref number to match the data block in xennet driver and xenvif driver).

Quoted 9 lines

Now regarding the results, I can see the timestamp 3842008681 for xennet_notify_tx_irq, 3842008885 for vgic_inject_irq, and 3842008935 for vcpu_kick. Where is the corresponding TSC for the domain receiving the notification?

Also for the other case, starting at 3842016505, can you please highlight the timestamp for vgic_inject_irq, vcpu_kick, and also the one for the domain receiving the notification?

Quoted 8 lines

The most interesting timestamps would be the timestamp for vcpu_kick in "notification sending domain" [a], the timestamp for receiving the interrupt in the Xen on pCPU for the "notification receiving domain" [b], and the timestamp for the "notification receiving domain" getting the notification [c].

If really context switch is the issue, then the interesting latency would be between [a] and [b].

Understand. I agree that I didn't move into more details, the main reason is Xen dmesg buffer is fragile after adding more logs, e.g. after I added log in the function gicv3_send_sgi(), Xen will stuck during the booting phase, and after adding logs in leave_hypervisor_to_guest() it will introduce huge logs (so I need to only trace for first 16 CPUs to mitigate log flood).

Quoted 2 lines

I think it would be better to enable xentrace for my profiling at my side. If I have any further data, will share back.

Looking forward to it. Without more details it is impossible to identify the source of the problem and fix it.

author
#13 lore raw

Hi Stefano,

Quoted 18 lines · Stefano Stabellini
Quoted 8 lines · Leo Yan
Quoted 3 lines

If you are really running with the NULL scheduler, then I would investigate why the vCPU has is_running == 0 because it should not happen.

Correct for this: it's my bad that I didn't really enable NULL scheduler in my code base. After I enabled NULL scheduler, the latency by context switching is dismissed.

Quoted 8 lines
 8963              pub-338   [002]   217.777652: bprint:               xennet_tx_setup_grant: id=60 ref=1340 offset=2 len=1514 TSC: 7892178799
 8964              pub-338   [002]   217.777662: bprint:               xennet_tx_setup_grant: id=82 ref=1362 offset=2050 len=1006 TSC: 7892179043
 8965     ksoftirqd/12-75    [012]   255.466914: bprint:               xenvif_tx_build_gops.constprop.0: id=60 ref=1340 offset=2 len=1514 TSC: 7892179731
 8966     ksoftirqd/12-75    [012]   255.466915: bprint:               xenvif_tx_build_gops.constprop.0: id=82 ref=1362 offset=2050 len=1006 TSC: 7892179761
 8967              pub-338   [002]   217.778057: bprint:               xennet_tx_setup_grant: id=60 ref=1340 offset=2050 len=1514 TSC: 7892188930
 8968              pub-338   [002]   217.778072: bprint:               xennet_tx_setup_grant: id=53 ref=1333 offset=2 len=1514 TSC: 7892189293
 8969       containerd-2965  [012]   255.467304: bprint:               xenvif_tx_build_gops.constprop.0: id=60 ref=1340 offset=2050 len=1514 TSC: 7892189479
 8970       containerd-2965  [012]   255.467306: bprint:               xenvif_tx_build_gops.constprop.0: id=53 ref=1333 offset=2 len=1514 TSC: 7892189533
Quoted 2 lines

I am having difficulty following the messages. Are the two points [a] and [b] as described in the previous email shown here?

No, the [b] point is about the trace point in Xen hypvervisor.

For easier review I combined the trace log from Xen dom0 and dom0 Linux kernels, in above log, the function xennet_tx_setup_grant() is traced from domU Linux kernel and the function xenvif_tx_build_gops.constprop.0 from dom0 Linux kernel and I sorted the logs with TSC (Arm arch timer physical counter).

Sorry for confusion and I didn't explain clearly.

Quoted 37 lines
Quoted 37 lines

So the xennet (Xen net forend driver) and xenvif (net backend driver) work in parallel. Please note, I didn't see networking performance improvement after changed to use NULL scheduler.

Now I will compare the duration for two directions, one direction is sending data from xennet to xenvif, and another is the reversed direction. It's very likely the two directions have significant difference for sending data with grant tables, you could see in above log, it takes 20~30us to transmit a data block (we can use the id number and grant table's ref number to match the data block in xennet driver and xenvif driver).

Quoted 9 lines

Now regarding the results, I can see the timestamp 3842008681 for xennet_notify_tx_irq, 3842008885 for vgic_inject_irq, and 3842008935 for vcpu_kick. Where is the corresponding TSC for the domain receiving the notification?

Also for the other case, starting at 3842016505, can you please highlight the timestamp for vgic_inject_irq, vcpu_kick, and also the one for the domain receiving the notification?

Quoted 8 lines

The most interesting timestamps would be the timestamp for vcpu_kick in "notification sending domain" [a], the timestamp for receiving the interrupt in the Xen on pCPU for the "notification receiving domain" [b], and the timestamp for the "notification receiving domain" getting the notification [c].

If really context switch is the issue, then the interesting latency would be between [a] and [b].

Understand. I agree that I didn't move into more details, the main reason is Xen dmesg buffer is fragile after adding more logs, e.g. after I added log in the function gicv3_send_sgi(), Xen will stuck during the booting phase, and after adding logs in leave_hypervisor_to_guest() it will introduce huge logs (so I need to only trace for first 16 CPUs to mitigate log flood).

Quoted 5 lines
Quoted 2 lines

I think it would be better to enable xentrace for my profiling at my side. If I have any further data, will share back.

Looking forward to it. Without more details it is impossible to identify the source of the problem and fix it.

The application passes data to Linux kernel and low level driver needs to transfer skbs between Xen network frontend and backend drivers via Xen shared ring buffer / grant table. So let's step back to review the duration in the network drivers for sending and receiving skbs.

I measured intervals for sending skbs, one direction is for sending skb from dom0 to domU and another direction is from domU to dom0. For easier review, I plotted the intervals in diagrams [1][2] for these two directions respectively.

In a diagram it has three metrics:

- Packet sending interval: measures the interval from the sender.

For the direction from dom0 to domu, this metric measures the interval for sending skbs in the Xen backend driver in dom0.

For the direction from domU to dom0, this metric measures the interval for sending skbs in the Xen frontend driver in domU.

- Packet receiving interval: measures the interval for the receiver.

For the direction from dom0 to domu, this metric measures the interval for receiving skbs in the Xen frontend driver in domU.

For the direction from domU to dom0, this metric measures the interval for receiving skbs in the Xen backend driver in dom0.

- Packet response interval: measures the interval between sender and receiver.

This interval measures the duration between a skb is sending out from the sender and until it's fetched from grant table by the receiver.

From these three metrics we can know the latency is caused by three
actors: sender, receiver, Xen hypervisor.

In two diagrams we can see the packet response interval (yellow line) is about 10~20us. The response interval for domu -> dom0 (~10us) is even better than the interval for dom0 -> domu (15~20us), which means the response time is not the curplit for the poor performance when sending data from domu to dom0.

The purple line and blue line for sending and receiving intervals are correlated, this means the drivers in two sides (dom0 and domU) work in parallel.

A big difference is we can see the sending interval (purple line) in the diagram xmit_interval_dom0_to_domu.png usually keeps at ~1us, but the sending interval in the diagram xmit_interval_domu_to_dom0.png is about 10us.

If map to the code, I think the function xennet_start_xmit() in Xen frontend driver is critical for the sending interval in domU. I can see several things cost time when sending a packet:

- Xen frontend driver needs to setup grant table for every skb, it invokes the function xennet_tx_setup_grant() and gnttab_grant_foreign_access_ref() to prepare grant table;

- Xen frontend driver sends notification by calling notify_remote_via_irq(). It will trap to Xen hypervisor to send the interrupt, this takes several macro seonds for this step.

- Xen frontend driver calls xennet_tx_buf_gc(), the interval for this function is vary, it will take ~30us in the case for reclaiming grant table.

Any thoughts for this?

Thanks,
Leo

[1] https://people.linaro.org/~leo.yan/xen/xmit_interval_dom0_to_domu.png
[2] https://people.linaro.org/~leo.yan/xen/xmit_interval_domu_to_dom0.png

P.s. I will take other priority tasks from next week, it's pity that I cannot fix the issue at this point. Just want you know I will take this task as low priority, but if you have any idea I can give a try or test at my side.

author
#14 lore raw

On Fri, Oct 28, 2022 at 05:54:05PM +0800, Leo Yan wrote:

[...]

Quoted 12 lines

If map to the code, I think the function xennet_start_xmit() in Xen frontend driver is critical for the sending interval in domU. I can see several things cost time when sending a packet:

- Xen frontend driver needs to setup grant table for every skb, it invokes the function xennet_tx_setup_grant() and gnttab_grant_foreign_access_ref() to prepare grant table;

- Xen frontend driver sends notification by calling notify_remote_via_irq(). It will trap to Xen hypervisor to send the interrupt, this takes several macro seonds for this step.

Quoted 5 lines

- Xen frontend driver calls xennet_tx_buf_gc(), the interval for this function is vary, it will take ~30us in the case for reclaiming grant table.

Any thoughts for this?

Supplement info with Ftrace function graph. You could see below log which shows the time is spent in xennet_start_xmit():

 1)               |  xennet_start_xmit() {
 1)               |  /* xennet_start_xmit: TSC: 50702652609 */
 1)   0.240 us    |    _raw_spin_lock_irqsave();
 1)               |    netif_skb_features() {
 1)   0.280 us    |      skb_network_protocol();
 1)   0.920 us    |    }
 1)               |    gnttab_foreach_grant_in_range() {
 1)               |      xennet_tx_setup_grant() {
 1)   0.280 us    |        gnttab_claim_grant_reference();
 1)               |        gnttab_grant_foreign_access_ref() {
 1)   0.280 us    |          gnttab_update_entry_v1();
 1)   0.800 us    |        }
 1)               |        /* id=103 ref=871 offset=2050 len=1514 TSC: 50702652709 */
 1)   2.200 us    |      }
 1)   2.800 us    |    }
 1)               |  /* xennet_notify_tx_irq: TSC: 50702652741 */
 1)               |    notify_remote_via_irq() {
 1)               |      irq_get_irq_data() {
 1)   0.240 us    |        irq_to_desc();
 1)   0.760 us    |      }
 1)   3.880 us    |    }
 1)   0.280 us    |    xennet_tx_buf_gc();
 1)   0.240 us    |    _raw_spin_unlock_irqrestore();
 1) + 11.120 us   |  }
 1)               |  xennet_start_xmit() {
 1)               |  /* xennet_start_xmit: TSC: 50702652974 */
 1)   0.280 us    |    _raw_spin_lock_irqsave();
 1)               |    netif_skb_features() {
 1)   0.240 us    |      skb_network_protocol();
 1)   0.760 us    |    }
 1)               |    gnttab_foreach_grant_in_range() {
 1)               |      xennet_tx_setup_grant() {
 1)   0.280 us    |        gnttab_claim_grant_reference();
 1)               |        gnttab_grant_foreign_access_ref() {
 1)   0.360 us    |          gnttab_update_entry_v1();
 1)   0.840 us    |        }
 1)               |        /* id=101 ref=869 offset=1026 len=574 TSC: 50702653093 */
 1)   2.800 us    |      }
 1)   3.320 us    |    }
 1)               |  /* xennet_notify_tx_irq: TSC: 50702653124 */
 1)               |    notify_remote_via_irq() {
 1)               |      irq_get_irq_data() {
 1)   0.240 us    |        irq_to_desc();
 1)   0.760 us    |      }
 1)   3.840 us    |    }
 1)   0.280 us    |    xennet_tx_buf_gc();
 1)   0.280 us    |    _raw_spin_unlock_irqrestore();
 1) + 11.800 us   |  }
 1)               |  /* finish_transmit_data */
 1)               |  /* transmit_data */
 1)               |  /* finish_transmit_data */
 1)               |  /* transmit_data */
 1)               |  /* finish_transmit_data */
 1)               |  /* transmit_data */
 1)               |  /* finish_transmit_data */
 1)               |  /* Before_throttle */
 1)               |  xennet_start_xmit() {
 1)               |  /* xennet_start_xmit: TSC: 50702654697 */
 1)   0.280 us    |    _raw_spin_lock_irqsave();
 1)               |    netif_skb_features() {
 1)   0.240 us    |      skb_network_protocol();
 1)   0.760 us    |    }
 1)               |    gnttab_foreach_grant_in_range() {
 1)               |      xennet_tx_setup_grant() {
 1)   0.280 us    |        gnttab_claim_grant_reference();
 1)               |        gnttab_grant_foreign_access_ref() {
 1)   0.320 us    |          gnttab_update_entry_v1();
 1)   0.920 us    |        }
 1)               |        /* id=3 ref=771 offset=2050 len=1514 TSC: 50702654801 */
 1)   2.400 us    |      }
 1)   2.960 us    |    }
 1)               |  /* xennet_notify_tx_irq: TSC: 50702654832 */
 1)               |    notify_remote_via_irq() {
 1)               |      irq_get_irq_data() {
 1)   0.280 us    |        irq_to_desc();
 1)   0.760 us    |      }
 1)   4.160 us    |    }
 1)               |    xennet_tx_buf_gc() {
 1)               |      gnttab_end_foreign_access_ref() {
 1)   0.480 us    |        gnttab_end_foreign_access_ref_v1();
 1)   0.960 us    |      }
 1)   0.280 us    |      gnttab_release_grant_reference();
 1)               |      __dev_kfree_skb_irq() {
 1)   0.240 us    |        __raise_softirq_irqoff();
 1)   1.240 us    |      }
 1)               |      gnttab_end_foreign_access_ref() {
 1)   0.480 us    |        gnttab_end_foreign_access_ref_v1();
 1)   0.960 us    |      }
 1)   0.240 us    |      gnttab_release_grant_reference();
 1)               |      __dev_kfree_skb_irq() {
 1)   0.240 us    |        __raise_softirq_irqoff();
 1)   0.760 us    |      }
 1)               |      gnttab_end_foreign_access_ref() {
 1)   0.400 us    |        gnttab_end_foreign_access_ref_v1();
 1)   0.840 us    |      }
 1)   0.280 us    |      gnttab_release_grant_reference();
 1)               |      __dev_kfree_skb_irq() {
 1)   0.280 us    |        __raise_softirq_irqoff();
 1)   0.880 us    |      }
 1)               |      gnttab_end_foreign_access_ref() {
 1)   0.360 us    |        gnttab_end_foreign_access_ref_v1();
 1)   0.840 us    |      }
 1)   0.240 us    |      gnttab_release_grant_reference();
 1)               |      __dev_kfree_skb_irq() {
 1)   0.240 us    |        __raise_softirq_irqoff();
 1)   0.760 us    |      }
 1)               |      gnttab_end_foreign_access_ref() {
 1)   0.240 us    |        gnttab_end_foreign_access_ref_v1();
 1)   0.760 us    |      }
 1)   0.240 us    |      gnttab_release_grant_reference();
 1)               |      __dev_kfree_skb_irq() {
 1)   0.240 us    |        __raise_softirq_irqoff();
 1)   0.760 us    |      }
 1)               |      gnttab_end_foreign_access_ref() {
 1)   0.360 us    |        gnttab_end_foreign_access_ref_v1();
 1)   0.960 us    |      }
 1)   0.240 us    |      gnttab_release_grant_reference();
 1)               |      __dev_kfree_skb_irq() {
 1)   0.240 us    |        __raise_softirq_irqoff();
 1)   0.760 us    |      }
 1) + 17.720 us   |    }
 1)   0.360 us    |    _raw_spin_unlock_irqrestore();
 1) + 29.320 us   |  }
 1)               |  xennet_start_xmit() {
 1)               |  /* xennet_start_xmit: TSC: 50702655824 */
 1)   0.360 us    |    _raw_spin_lock_irqsave();
 1)               |    netif_skb_features() {
 1)   0.280 us    |      skb_network_protocol();
 1)   0.760 us    |    }
 1)               |    gnttab_foreach_grant_in_range() {
 1)               |      xennet_tx_setup_grant() {
 1)   0.280 us    |        gnttab_claim_grant_reference();
 1)               |        gnttab_grant_foreign_access_ref() {
 1)   0.280 us    |          gnttab_update_entry_v1();
 1)   0.880 us    |        }
 1)               |        /* id=28 ref=796 offset=2 len=1514 TSC: 50702655925 */
 1)   2.280 us    |      }
 1)   2.920 us    |    }
 1)               |  /* xennet_notify_tx_irq: TSC: 50702655958 */
 1)               |    notify_remote_via_irq() {
 1)               |      irq_get_irq_data() {
 1)   0.280 us    |        irq_to_desc();
 1)   0.760 us    |      }
 1)   4.160 us    |    }
 1)               |    xennet_tx_buf_gc() {
 1)               |      gnttab_end_foreign_access_ref() {
 1)   0.360 us    |        gnttab_end_foreign_access_ref_v1();
 1)   0.880 us    |      }
 1)   0.280 us    |      gnttab_release_grant_reference();
 1)               |      __dev_kfree_skb_irq() {
 1)   0.280 us    |        __raise_softirq_irqoff();
 1)   1.280 us    |      }
 1)               |      gnttab_end_foreign_access_ref() {
 1)   0.360 us    |        gnttab_end_foreign_access_ref_v1();
 1)   0.880 us    |      }
 1)   0.760 us    |      gnttab_release_grant_reference();
 1)               |      __dev_kfree_skb_irq() {
 1)   0.280 us    |        __raise_softirq_irqoff();
 1)   0.760 us    |      }
 1)               |      gnttab_end_foreign_access_ref() {
 1)   0.400 us    |        gnttab_end_foreign_access_ref_v1();
 1)   0.880 us    |      }
 1)   0.240 us    |      gnttab_release_grant_reference();
 1)               |      __dev_kfree_skb_irq() {
 1)   0.400 us    |        __raise_softirq_irqoff();
 1)   0.880 us    |      }
 1)               |      gnttab_end_foreign_access_ref() {
 1)   0.400 us    |        gnttab_end_foreign_access_ref_v1();
 1)   0.880 us    |      }
 1)   0.280 us    |      gnttab_release_grant_reference();
 1)               |      __dev_kfree_skb_irq() {
 1)   0.240 us    |        __raise_softirq_irqoff();
 1)   0.760 us    |      }
 1) + 12.440 us   |    }
 1)   0.800 us    |    _raw_spin_unlock_irqrestore();
 1) + 24.080 us   |  }
 1)               |  xennet_start_xmit() {
 1)               |  /* xennet_start_xmit: TSC: 50702656728 */
 1)   0.240 us    |    _raw_spin_lock_irqsave();
 1)               |    netif_skb_features() {
 1)   0.240 us    |      skb_network_protocol();
 1)   0.760 us    |    }
 1)               |    gnttab_foreach_grant_in_range() {
 1)               |      xennet_tx_setup_grant() {
 1)   0.280 us    |        gnttab_claim_grant_reference();
 1)               |        gnttab_grant_foreign_access_ref() {
 1)   0.240 us    |          gnttab_update_entry_v1();
 1)   0.720 us    |        }
 1)               |        /* id=103 ref=871 offset=2050 len=1422 TSC: 50702656823 */
 1)   2.160 us    |      }
 1)   2.800 us    |    }
 1)               |  /* xennet_notify_tx_irq: TSC: 50702656850 */
 1)               |    notify_remote_via_irq() {
 1)               |      irq_get_irq_data() {
 1)   0.240 us    |        irq_to_desc();
 1)   0.720 us    |      }
 1)   3.920 us    |    }
 1)   0.320 us    |    xennet_tx_buf_gc();
 1)   0.280 us    |    _raw_spin_unlock_irqrestore();
 1) + 11.000 us   |  }
#15 lore raw
In reply to Leo Yan

Hi Leo,

Thank you for the fantastic work with so many good details. Apologies for the late reply -- I have been traveling and on holiday.

Quoted 112 lines · Leo Yan

Hi Stefano,

Quoted 18 lines · Stefano Stabellini
Quoted 8 lines · Leo Yan
Quoted 3 lines

If you are really running with the NULL scheduler, then I would investigate why the vCPU has is_running == 0 because it should not happen.

Correct for this: it's my bad that I didn't really enable NULL scheduler in my code base. After I enabled NULL scheduler, the latency by context switching is dismissed.

Quoted 8 lines
 8963              pub-338   [002]   217.777652: bprint:               xennet_tx_setup_grant: id=60 ref=1340 offset=2 len=1514 TSC: 7892178799
 8964              pub-338   [002]   217.777662: bprint:               xennet_tx_setup_grant: id=82 ref=1362 offset=2050 len=1006 TSC: 7892179043
 8965     ksoftirqd/12-75    [012]   255.466914: bprint:               xenvif_tx_build_gops.constprop.0: id=60 ref=1340 offset=2 len=1514 TSC: 7892179731
 8966     ksoftirqd/12-75    [012]   255.466915: bprint:               xenvif_tx_build_gops.constprop.0: id=82 ref=1362 offset=2050 len=1006 TSC: 7892179761
 8967              pub-338   [002]   217.778057: bprint:               xennet_tx_setup_grant: id=60 ref=1340 offset=2050 len=1514 TSC: 7892188930
 8968              pub-338   [002]   217.778072: bprint:               xennet_tx_setup_grant: id=53 ref=1333 offset=2 len=1514 TSC: 7892189293
 8969       containerd-2965  [012]   255.467304: bprint:               xenvif_tx_build_gops.constprop.0: id=60 ref=1340 offset=2050 len=1514 TSC: 7892189479
 8970       containerd-2965  [012]   255.467306: bprint:               xenvif_tx_build_gops.constprop.0: id=53 ref=1333 offset=2 len=1514 TSC: 7892189533
Quoted 2 lines

I am having difficulty following the messages. Are the two points [a] and [b] as described in the previous email shown here?

No, the [b] point is about the trace point in Xen hypvervisor.

For easier review I combined the trace log from Xen dom0 and dom0 Linux kernels, in above log, the function xennet_tx_setup_grant() is traced from domU Linux kernel and the function xenvif_tx_build_gops.constprop.0 from dom0 Linux kernel and I sorted the logs with TSC (Arm arch timer physical counter).

Sorry for confusion and I didn't explain clearly.

Quoted 37 lines
Quoted 37 lines

So the xennet (Xen net forend driver) and xenvif (net backend driver) work in parallel. Please note, I didn't see networking performance improvement after changed to use NULL scheduler.

Now I will compare the duration for two directions, one direction is sending data from xennet to xenvif, and another is the reversed direction. It's very likely the two directions have significant difference for sending data with grant tables, you could see in above log, it takes 20~30us to transmit a data block (we can use the id number and grant table's ref number to match the data block in xennet driver and xenvif driver).

Quoted 9 lines

Now regarding the results, I can see the timestamp 3842008681 for xennet_notify_tx_irq, 3842008885 for vgic_inject_irq, and 3842008935 for vcpu_kick. Where is the corresponding TSC for the domain receiving the notification?

Also for the other case, starting at 3842016505, can you please highlight the timestamp for vgic_inject_irq, vcpu_kick, and also the one for the domain receiving the notification?

Quoted 8 lines

The most interesting timestamps would be the timestamp for vcpu_kick in "notification sending domain" [a], the timestamp for receiving the interrupt in the Xen on pCPU for the "notification receiving domain" [b], and the timestamp for the "notification receiving domain" getting the notification [c].

If really context switch is the issue, then the interesting latency would be between [a] and [b].

Understand. I agree that I didn't move into more details, the main reason is Xen dmesg buffer is fragile after adding more logs, e.g. after I added log in the function gicv3_send_sgi(), Xen will stuck during the booting phase, and after adding logs in leave_hypervisor_to_guest() it will introduce huge logs (so I need to only trace for first 16 CPUs to mitigate log flood).

Quoted 5 lines
Quoted 2 lines

I think it would be better to enable xentrace for my profiling at my side. If I have any further data, will share back.

Looking forward to it. Without more details it is impossible to identify the source of the problem and fix it.

The application passes data to Linux kernel and low level driver needs to transfer skbs between Xen network frontend and backend drivers via Xen shared ring buffer / grant table. So let's step back to review the duration in the network drivers for sending and receiving skbs.

I measured intervals for sending skbs, one direction is for sending skb from dom0 to domU and another direction is from domU to dom0. For easier review, I plotted the intervals in diagrams [1][2] for these two directions respectively.

In a diagram it has three metrics:

- Packet sending interval: measures the interval from the sender.

For the direction from dom0 to domu, this metric measures the interval for sending skbs in the Xen backend driver in dom0.

For the direction from domU to dom0, this metric measures the interval for sending skbs in the Xen frontend driver in domU.

- Packet receiving interval: measures the interval for the receiver.

For the direction from dom0 to domu, this metric measures the interval for receiving skbs in the Xen frontend driver in domU.

For the direction from domU to dom0, this metric measures the interval for receiving skbs in the Xen backend driver in dom0.

- Packet response interval: measures the interval between sender and receiver.

This interval measures the duration between a skb is sending out from the sender and until it's fetched from grant table by the receiver.

Quoted 8 lines
Quoted 1 line

From these three metrics we can know the latency is caused by three

actors: sender, receiver, Xen hypervisor.

In two diagrams we can see the packet response interval (yellow line) is about 10~20us. The response interval for domu -> dom0 (~10us) is even better than the interval for dom0 -> domu (15~20us), which means the response time is not the curplit for the poor performance when sending data from domu to dom0.

I agree with you.

Quoted 8 lines

The purple line and blue line for sending and receiving intervals are correlated, this means the drivers in two sides (dom0 and domU) work in parallel.

A big difference is we can see the sending interval (purple line) in the diagram xmit_interval_dom0_to_domu.png usually keeps at ~1us, but the sending interval in the diagram xmit_interval_domu_to_dom0.png is about 10us.

I also find it curious that the receiving interval is almost 0 in the "dom0 to domU" direction and instead it is about 10us in the "domU to dom0" direction. Why would it take more time to receive skbs in one direction compared to the other?

Are the "receiving interval" and the "sending interval" measuring the same delay just from the receiver side and from the sender side?

Quoted 12 lines

If map to the code, I think the function xennet_start_xmit() in Xen frontend driver is critical for the sending interval in domU. I can see several things cost time when sending a packet:

- Xen frontend driver needs to setup grant table for every skb, it invokes the function xennet_tx_setup_grant() and gnttab_grant_foreign_access_ref() to prepare grant table;

- Xen frontend driver sends notification by calling notify_remote_via_irq(). It will trap to Xen hypervisor to send the interrupt, this takes several macro seonds for this step.

Quoted 5 lines

- Xen frontend driver calls xennet_tx_buf_gc(), the interval for this function is vary, it will take ~30us in the case for reclaiming grant table.

Any thoughts for this?

For the sake of latency, it is better to avoid sources of uncertainty (even at the cost of bandwidth). From what you wrote above and from the other email it would seem that xennet_tx_buf_gc() can cause significant delays.

One idea is only to parse the responses (RING_COPY_RESPONSE) in xennet_tx_buf_gc() and delay the calls to gnttab_end_foreign_access_ref for later. In other words:

1) xennet_tx_buf_gc: read responses, grants to be removed "for later"
2) independently when idle (maybe from a kthread or independent tasklet) look for grants to be removed and remove them

Basically it is a lazy grant removal scheme where once receiving skbs, the related grants are not immediately freed, but later lazily.