Re: [PATCH v3] Drivers: hv: vmbus: Expose counters for interrupts and full conditions

2019-01-21 Thread Kimberly Brown
On Thu, Jan 17, 2019 at 09:11:03AM -0800, Stephen Hemminger wrote:
> 
> 
> > +static ssize_t channel_intr_in_full_show(const struct vmbus_channel
> > *channel,
> > +char *buf)
> > +{
> > +   return sprintf(buf, "%llu\n", channel->intr_in_full);
> > +}
> 
> 
> intr_in_full is u64, which is not the same as unsigned long long.
> to be correct you need a cast here.
>

Thanks for the feedback. I'll fix this issue in all four of the "_show"
functions that are added in this patch.


> > > diff --git a/include/linux/hyperv.h b/include/linux/hyperv.h
> > > index dcb6977afce9..7e5239123276 100644
> > > --- a/include/linux/hyperv.h
> > > +++ b/include/linux/hyperv.h
> > > @@ -751,6 +751,27 @@ struct vmbus_channel {
> > >   u64 interrupts; /* Host to Guest interrupts */
> > >   u64 sig_events; /* Guest to Host events */
> > > 
> > > + /* Interrupt counts for 2 types of Guest to Host interrupts */
> > > + u64 intr_in_full;   /* in ring buffer, full to not full */
> > > + u64 intr_out_empty; /* out ring buffer, empty to not empty */
> > > +
> > > + /*
> > > +  * The total number of write operations that encountered a full
> > > +  * outbound ring buffer.
> > > +  */
> > > + u64 out_full_total;
> > > + /*
> > > +  * The number of write operations that were the first to encounter a
> > > +  * full outbound ring buffer.
> > > +  */
> > > + u64 out_full_first;
> 
> Adding more fields changes cache layout which can cause
> additional cache miss in the hot path.  
>

Good point. I think that the "intr_out_empty" field is in a good
location, but the "intr_in_full", "out_full_first", and "out_full_total"
fields could be moved to the end of the struct. These variables are used
only when ring buffer full conditions occur. Ring buffer full conditions
shouldn't be encountered often, and, if they are, they're a signal that
changes should probably be made to prevent them.

If you have any other suggestions for this, please let me know.


> > > + /*
> > > +  * Indicates that a full outbound ring buffer was encountered. The flag
> > > +  * is set to true when a full outbound ring buffer is encountered and
> > > +  * set to false when a write to the outbound ring buffer is completed.
> > > +  */
> > > + bool out_full_flag;
> 
> Discussion on kernel mailing list. Recommends against putting bool
> in structures since that pads to full sizeof(int).  Could this be
> part of a bitfield?
> 

There are currently 4 other bool variables in this struct. Maybe some or
all of the bool variables could be placed adjacent to each other and
changed into bitfields. I'll need to look into this.


> > >   /* Channel callback's invoked in softirq context */
> > >   struct tasklet_struct callback_event;
> > >   void (*onchannel_callback)(void *context);
> > > @@ -936,6 +957,23 @@ static inline void *get_per_channel_state(struct
> > > vmbus_channel *c)
> > >  static inline void set_channel_pending_send_size(struct vmbus_channel *c,
> > >u32 size)
> > >  {
> > > + unsigned long flags;
> > > +
> > > + spin_lock_irqsave(>outbound.ring_lock, flags);
> > > +
> > > + if (size) {
> > > + ++c->out_full_total;
> > > +
> > > + if (!c->out_full_flag) {
> > > + ++c->out_full_first;
> > > + c->out_full_flag = true;
> > > + }
> > > + } else {
> > > + c->out_full_flag = false;
> > > + }
> > > +
> > > + spin_unlock_irqrestore(>outbound.ring_lock, flags);
> 
> If this is called often, the additional locking will impact performance.
>

In hv_sock, each call of "hvs_stream_has_space()" results in a call to
"channel_set_pending_send_size()", so this could be a concern. I'll work
on addressing this issue.


> > >   c->outbound.ring_buffer->pending_send_sz = size;
> > >  }
> > > 
> 
> Could I propose another alternative.
> 
> It might be more useful to count the guest to host interaction events
> rather than the ring buffer.
> 
> For example the number of calls to:
>   vmbus_set_event which means host exit call
>   vmbus_setevent fastpath using sync_set_bit
>   calls to rinbuffer_write that returned -EAGAIN
> 
> These would require less locking, reuse existing code paths
> and not require additional state.
> 

I'm not sure that this approach would provide the data that we're
looking for. For example, we're interested in evaluating how often ring
buffer write operations encounter full conditions. For this, we need to
know how many interaction events were caused by the ring buffer being
full. Counting the number of calls to "vmbus_set_event()" and
"vmbus_setevent()" wouldn't allow us to determine what caused the events.

For counting the full conditions, the number of calls to
"ring_buffer_write()" that returned -EAGAIN isn't sufficient because
hv_sock doesn't use the -EAGAIN path to determine that the ring buffer is
full. Therefore, we need to count the number of full conditions in both

Re: [PATCH v3] Drivers: hv: vmbus: Expose counters for interrupts and full conditions

2019-01-17 Thread Stephen Hemminger



> +static ssize_t channel_intr_in_full_show(const struct vmbus_channel
> *channel,
> +  char *buf)
> +{
> + return sprintf(buf, "%llu\n", channel->intr_in_full);
> +}


intr_in_full is u64, which is not the same as unsigned long long.
to be correct you need a cast here.

> > diff --git a/include/linux/hyperv.h b/include/linux/hyperv.h
> > index dcb6977afce9..7e5239123276 100644
> > --- a/include/linux/hyperv.h
> > +++ b/include/linux/hyperv.h
> > @@ -751,6 +751,27 @@ struct vmbus_channel {
> > u64 interrupts; /* Host to Guest interrupts */
> > u64 sig_events; /* Guest to Host events */
> > 
> > +   /* Interrupt counts for 2 types of Guest to Host interrupts */
> > +   u64 intr_in_full;   /* in ring buffer, full to not full */
> > +   u64 intr_out_empty; /* out ring buffer, empty to not empty */
> > +
> > +   /*
> > +* The total number of write operations that encountered a full
> > +* outbound ring buffer.
> > +*/
> > +   u64 out_full_total;
> > +   /*
> > +* The number of write operations that were the first to encounter a
> > +* full outbound ring buffer.
> > +*/
> > +   u64 out_full_first;

Adding more fields changes cache layout which can cause
additional cache miss in the hot path.  

> > +   /*
> > +* Indicates that a full outbound ring buffer was encountered. The flag
> > +* is set to true when a full outbound ring buffer is encountered and
> > +* set to false when a write to the outbound ring buffer is completed.
> > +*/
> > +   bool out_full_flag;

Discussion on kernel mailing list. Recommends against putting bool
in structures since that pads to full sizeof(int).  Could this be
part of a bitfield?

> > /* Channel callback's invoked in softirq context */
> > struct tasklet_struct callback_event;
> > void (*onchannel_callback)(void *context);
> > @@ -936,6 +957,23 @@ static inline void *get_per_channel_state(struct
> > vmbus_channel *c)
> >  static inline void set_channel_pending_send_size(struct vmbus_channel *c,
> >  u32 size)
> >  {
> > +   unsigned long flags;
> > +
> > +   spin_lock_irqsave(>outbound.ring_lock, flags);
> > +
> > +   if (size) {
> > +   ++c->out_full_total;
> > +
> > +   if (!c->out_full_flag) {
> > +   ++c->out_full_first;
> > +   c->out_full_flag = true;
> > +   }
> > +   } else {
> > +   c->out_full_flag = false;
> > +   }
> > +
> > +   spin_unlock_irqrestore(>outbound.ring_lock, flags);

If this is called often, the additional locking will impact performance.

> > c->outbound.ring_buffer->pending_send_sz = size;
> >  }
> > 

Could I propose another alternative.

It might be more useful to count the guest to host interaction events
rather than the ring buffer.

For example the number of calls to:
vmbus_set_event which means host exit call
vmbus_setevent fastpath using sync_set_bit
calls to rinbuffer_write that returned -EAGAIN

These would require less locking, reuse existing code paths
and not require additional state.

___
devel mailing list
de...@linuxdriverproject.org
http://driverdev.linuxdriverproject.org/mailman/listinfo/driverdev-devel


RE: [PATCH v3] Drivers: hv: vmbus: Expose counters for interrupts and full conditions

2019-01-17 Thread Michael Kelley
From: Kimberly Brown  Sent: Wednesday, January 16, 2019 
8:38 PM
> 
> Counter values for per-channel interrupts and ring buffer full
> conditions are useful for investigating performance.
> 
> Expose counters in sysfs for 2 types of guest to host interrupts:
> 1) Interrupts caused by the channel's outbound ring buffer transitioning
> from empty to not empty
> 2) Interrupts caused by the channel's inbound ring buffer transitioning
> from full to not full while a packet is waiting for enough buffer space to
> become available
> 
> Expose 2 counters in sysfs for the number of times that write operations
> encountered a full outbound ring buffer:
> 1) The total number of write operations that encountered a full
> condition
> 2) The number of write operations that were the first to encounter a
> full condition
> 
> I tested this patch by confirming that the sysfs files were created and
> observing the counter values. The values seemed to increase by a
> reasonable amount when the Hyper-v related drivers were in use.
> 
> Signed-off-by: Kimberly Brown 
> ---
> Changes in v3:
>  - Used the outbound ring buffer spinlock to protect the the full
>condition counters in set_channel_pending_send_size()
>  - Corrected the KernelVersion values for the new entries in
>Documentation/ABI/stable/sysfs-bus-vmbus
> 
> Changes in v2:
>  - Added mailing lists to the cc list
>  - Removed the host to guest interrupt counters proposed in v1 because
>they were not accurate
>  - Added full condition counters for the channel's outbound ring buffer
> 
>  Documentation/ABI/stable/sysfs-bus-vmbus | 33 
>  drivers/hv/ring_buffer.c | 14 -
>  drivers/hv/vmbus_drv.c   | 32 
>  include/linux/hyperv.h   | 38 
>  4 files changed, 116 insertions(+), 1 deletion(-)
> 

Reviewed-by: Michael Kelley 
___
devel mailing list
de...@linuxdriverproject.org
http://driverdev.linuxdriverproject.org/mailman/listinfo/driverdev-devel


RE: [PATCH v3] Drivers: hv: vmbus: Expose counters for interrupts and full conditions

2019-01-16 Thread Dexuan Cui
> From: Kimberly Brown 
> Sent: Wednesday, January 16, 2019 8:38 PM
> To: Michael Kelley ; Long Li
> ; Sasha Levin ;
> Dexuan Cui 
> Cc: KY Srinivasan ; Haiyang Zhang
> ; Stephen Hemminger
> ; de...@linuxdriverproject.org;
> linux-ker...@vger.kernel.org
> Subject: [PATCH v3] Drivers: hv: vmbus: Expose counters for interrupts and 
> full
> conditions
> 
> Counter values for per-channel interrupts and ring buffer full
> conditions are useful for investigating performance.
> 
> Expose counters in sysfs for 2 types of guest to host interrupts:
> 1) Interrupts caused by the channel's outbound ring buffer transitioning
> from empty to not empty
> 2) Interrupts caused by the channel's inbound ring buffer transitioning
> from full to not full while a packet is waiting for enough buffer space to
> become available
> 
> Expose 2 counters in sysfs for the number of times that write operations
> encountered a full outbound ring buffer:
> 1) The total number of write operations that encountered a full
> condition
> 2) The number of write operations that were the first to encounter a
> full condition
> 
> I tested this patch by confirming that the sysfs files were created and
> observing the counter values. The values seemed to increase by a
> reasonable amount when the Hyper-v related drivers were in use.
> 
> Signed-off-by: Kimberly Brown 
> ---
> Changes in v3:
>  - Used the outbound ring buffer spinlock to protect the the full
>condition counters in set_channel_pending_send_size()
>  - Corrected the KernelVersion values for the new entries in
>Documentation/ABI/stable/sysfs-bus-vmbus
> 
> Changes in v2:
>  - Added mailing lists to the cc list
>  - Removed the host to guest interrupt counters proposed in v1 because
>they were not accurate
>  - Added full condition counters for the channel's outbound ring buffer
> 
>  Documentation/ABI/stable/sysfs-bus-vmbus | 33 
>  drivers/hv/ring_buffer.c | 14 -
>  drivers/hv/vmbus_drv.c   | 32 
>  include/linux/hyperv.h   | 38
> 
>  4 files changed, 116 insertions(+), 1 deletion(-)
> 
> diff --git a/Documentation/ABI/stable/sysfs-bus-vmbus
> b/Documentation/ABI/stable/sysfs-bus-vmbus
> index 3fed8fdb873d..a0304c563467 100644
> --- a/Documentation/ABI/stable/sysfs-bus-vmbus
> +++ b/Documentation/ABI/stable/sysfs-bus-vmbus
> @@ -146,3 +146,36 @@ KernelVersion:   4.16
>  Contact: Stephen Hemminger 
>  Description: Binary file created by uio_hv_generic for ring buffer
>  Users:   Userspace drivers
> +
> +What:
> /sys/bus/vmbus/devices//channels//intr_in_full
> +Date:   January 2019
> +KernelVersion:  5.0
> +Contact:Michael Kelley 
> +Description:Number of guest to host interrupts caused by the inbound
> ring
> + buffer transitioning from full to not full while a packet is
> + waiting for buffer space to become available
> +Users:  Debugging tools
> +
> +What:
> /sys/bus/vmbus/devices//channels//intr_out_empty
> +Date:   January 2019
> +KernelVersion:  5.0
> +Contact:Michael Kelley 
> +Description:Number of guest to host interrupts caused by the outbound
> ring
> + buffer transitioning from empty to not empty
> +Users:  Debugging tools
> +
> +What:
> /sys/bus/vmbus/devices//channels//out_full_first
> +Date:   January 2019
> +KernelVersion:  5.0
> +Contact:Michael Kelley 
> +Description:Number of write operations that were the first to encounter
> an
> + outbound ring buffer full condition
> +Users:  Debugging tools
> +
> +What:
> /sys/bus/vmbus/devices//channels//out_full_total
> +Date:   January 2019
> +KernelVersion:  5.0
> +Contact:Michael Kelley 
> +Description:Total number of write operations that encountered an
> outbound
> + ring buffer full condition
> +Users:  Debugging tools
> diff --git a/drivers/hv/ring_buffer.c b/drivers/hv/ring_buffer.c
> index 1f1a55e07733..9e8b31ccc142 100644
> --- a/drivers/hv/ring_buffer.c
> +++ b/drivers/hv/ring_buffer.c
> @@ -74,8 +74,10 @@ static void hv_signal_on_write(u32 old_write, struct
> vmbus_channel *channel)
>* This is the only case we need to signal when the
>* ring transitions from being empty to non-empty.
>*/
> - if (old_write == READ_ONCE(rbi->ring_buffer->read_index))
> + if (old_write == READ_ONCE(rbi->ring_buffer->read_index)) {
> + ++channel->intr_out_empty;
>   vmbus_setevent(channel);

[PATCH v3] Drivers: hv: vmbus: Expose counters for interrupts and full conditions

2019-01-16 Thread Kimberly Brown
Counter values for per-channel interrupts and ring buffer full
conditions are useful for investigating performance.

Expose counters in sysfs for 2 types of guest to host interrupts:
1) Interrupts caused by the channel's outbound ring buffer transitioning
from empty to not empty
2) Interrupts caused by the channel's inbound ring buffer transitioning
from full to not full while a packet is waiting for enough buffer space to
become available

Expose 2 counters in sysfs for the number of times that write operations
encountered a full outbound ring buffer:
1) The total number of write operations that encountered a full
condition
2) The number of write operations that were the first to encounter a
full condition

I tested this patch by confirming that the sysfs files were created and
observing the counter values. The values seemed to increase by a
reasonable amount when the Hyper-v related drivers were in use.

Signed-off-by: Kimberly Brown 
---
Changes in v3:
 - Used the outbound ring buffer spinlock to protect the the full
   condition counters in set_channel_pending_send_size()
 - Corrected the KernelVersion values for the new entries in
   Documentation/ABI/stable/sysfs-bus-vmbus

Changes in v2:
 - Added mailing lists to the cc list
 - Removed the host to guest interrupt counters proposed in v1 because
   they were not accurate
 - Added full condition counters for the channel's outbound ring buffer

 Documentation/ABI/stable/sysfs-bus-vmbus | 33 
 drivers/hv/ring_buffer.c | 14 -
 drivers/hv/vmbus_drv.c   | 32 
 include/linux/hyperv.h   | 38 
 4 files changed, 116 insertions(+), 1 deletion(-)

diff --git a/Documentation/ABI/stable/sysfs-bus-vmbus 
b/Documentation/ABI/stable/sysfs-bus-vmbus
index 3fed8fdb873d..a0304c563467 100644
--- a/Documentation/ABI/stable/sysfs-bus-vmbus
+++ b/Documentation/ABI/stable/sysfs-bus-vmbus
@@ -146,3 +146,36 @@ KernelVersion: 4.16
 Contact:   Stephen Hemminger 
 Description:   Binary file created by uio_hv_generic for ring buffer
 Users: Userspace drivers
+
+What:   /sys/bus/vmbus/devices//channels//intr_in_full
+Date:   January 2019
+KernelVersion:  5.0
+Contact:Michael Kelley 
+Description:Number of guest to host interrupts caused by the inbound ring
+   buffer transitioning from full to not full while a packet is
+   waiting for buffer space to become available
+Users:  Debugging tools
+
+What:   /sys/bus/vmbus/devices//channels//intr_out_empty
+Date:   January 2019
+KernelVersion:  5.0
+Contact:Michael Kelley 
+Description:Number of guest to host interrupts caused by the outbound ring
+   buffer transitioning from empty to not empty
+Users:  Debugging tools
+
+What:   /sys/bus/vmbus/devices//channels//out_full_first
+Date:   January 2019
+KernelVersion:  5.0
+Contact:Michael Kelley 
+Description:Number of write operations that were the first to encounter an
+   outbound ring buffer full condition
+Users:  Debugging tools
+
+What:   /sys/bus/vmbus/devices//channels//out_full_total
+Date:   January 2019
+KernelVersion:  5.0
+Contact:Michael Kelley 
+Description:Total number of write operations that encountered an outbound
+   ring buffer full condition
+Users:  Debugging tools
diff --git a/drivers/hv/ring_buffer.c b/drivers/hv/ring_buffer.c
index 1f1a55e07733..9e8b31ccc142 100644
--- a/drivers/hv/ring_buffer.c
+++ b/drivers/hv/ring_buffer.c
@@ -74,8 +74,10 @@ static void hv_signal_on_write(u32 old_write, struct 
vmbus_channel *channel)
 * This is the only case we need to signal when the
 * ring transitions from being empty to non-empty.
 */
-   if (old_write == READ_ONCE(rbi->ring_buffer->read_index))
+   if (old_write == READ_ONCE(rbi->ring_buffer->read_index)) {
+   ++channel->intr_out_empty;
vmbus_setevent(channel);
+   }
 }
 
 /* Get the next write location for the specified ring buffer. */
@@ -272,10 +274,19 @@ int hv_ringbuffer_write(struct vmbus_channel *channel,
 * is empty since the read index == write index.
 */
if (bytes_avail_towrite <= totalbytes_towrite) {
+   ++channel->out_full_total;
+
+   if (!channel->out_full_flag) {
+   ++channel->out_full_first;
+   channel->out_full_flag = true;
+   }
+
spin_unlock_irqrestore(_info->ring_lock, flags);
return -EAGAIN;
}
 
+   channel->out_full_flag = false;
+
/* Write to the ring buffer */
next_write_location = hv_get_next_write_location(outring_info);
 
@@ -530,6 +541,7 @@ void hv_pkt_iter_close(struct vmbus_channel *channel)
if (curr_write_sz <= pending_sz)