diff mbox

[XTF,v3] Implement pv_read_some

Message ID 20170720080942.4137-1-eggi.innovations@gmail.com (mailing list archive)
State New, archived
Headers show

Commit Message

Felix Schmoll July 20, 2017, 8:09 a.m. UTC
Implement reading from PV console. Making use of polling.

Signed-off-by: Felix Schmoll <eggi.innovations@gmail.com>

---
Changed since v2:
 * Change function name from pv_read to pv_read_some
 * Add comment to function name
 * Remove unnecessary code
---
 common/console.c      | 24 ++++++++++++++++++++++++
 include/xtf/console.h |  2 ++
 2 files changed, 26 insertions(+)

Comments

Andrew Cooper July 20, 2017, 9:40 a.m. UTC | #1
On 20/07/17 09:09, Felix Schmoll wrote:
> Implement reading from PV console. Making use of polling.
>
> Signed-off-by: Felix Schmoll <eggi.innovations@gmail.com>
>
> ---
> Changed since v2:
>   * Change function name from pv_read to pv_read_some
>   * Add comment to function name
>   * Remove unnecessary code
> ---
>   common/console.c      | 24 ++++++++++++++++++++++++
>   include/xtf/console.h |  2 ++
>   2 files changed, 26 insertions(+)
>
> diff --git a/common/console.c b/common/console.c
> index 7cb2361..1b79652 100644
> --- a/common/console.c
> +++ b/common/console.c
> @@ -46,6 +46,30 @@ static size_t pv_console_write_some(const char *buf, size_t len)
>   }
>   
>   /*
> + * Read out data from the pv ring, either until buffer is filled or no
> + * more data are available. Might result in partial strings, depending
> + * on how xenconsoled passes in data.

Much better.  Just one final question.  Do you intend this function to 
block until data becomes available?  (because that appears to be how it 
behaves.)

~Andrew

> + */
> +size_t pv_console_read_some(char *buf, size_t len)
> +{
> +    size_t s = 0;
> +    uint32_t cons, prod;
> +
> +    while ( !test_and_clear_bit(pv_evtchn, shared_info.evtchn_pending) ||
> +            (pv_ring->in_cons == pv_ring->in_prod) )
> +        hypercall_poll(pv_evtchn);
> +
> +    cons = pv_ring->in_cons, prod = LOAD_ACQUIRE(&pv_ring->in_prod);
> +
> +    while ( (s < len) && (0 < (prod - cons)) )
> +        buf[s++] = pv_ring->in[cons++ & (sizeof(pv_ring->in) - 1)];
> +
> +    STORE_RELEASE(&pv_ring->in_cons, cons);
> +
> +    return s;
> +}
> +
> +/*
>    * Write some data into the pv ring, synchronously waiting for all data to be
>    * consumed.
>    */
> diff --git a/include/xtf/console.h b/include/xtf/console.h
> index 2a93c06..caec790 100644
> --- a/include/xtf/console.h
> +++ b/include/xtf/console.h
> @@ -25,6 +25,8 @@ void init_pv_console(xencons_interface_t *ring,
>   void vprintk(const char *fmt, va_list args) __printf(1, 0);
>   void printk(const char *fmt, ...) __printf(1, 2);
>   
> +size_t pv_console_read_some(char *buf, size_t len);
> +
>   #endif /* XTF_CONSOLE_H */
>   
>   /*
Felix Schmoll July 21, 2017, 7:01 a.m. UTC | #2
>
>
> Much better.  Just one final question.  Do you intend this function to
> block until data becomes available?  (because that appears to be how it
> behaves.)
>
>
Yes. I could split it up into two functions if that bothers you. Or do you
just want me to include that in the comment?
Andrew Cooper July 21, 2017, 4:37 p.m. UTC | #3
On 21/07/17 08:01, Felix Schmoll wrote:
>
>
>     Much better.  Just one final question.  Do you intend this
>     function to block until data becomes available?  (because that
>     appears to be how it behaves.)
>
>
> Yes. I could split it up into two functions if that bothers you. Or do 
> you just want me to include that in the comment?

Just include it in the comment.

~Andrew
diff mbox

Patch

diff --git a/common/console.c b/common/console.c
index 7cb2361..1b79652 100644
--- a/common/console.c
+++ b/common/console.c
@@ -46,6 +46,30 @@  static size_t pv_console_write_some(const char *buf, size_t len)
 }
 
 /*
+ * Read out data from the pv ring, either until buffer is filled or no
+ * more data are available. Might result in partial strings, depending
+ * on how xenconsoled passes in data.
+ */
+size_t pv_console_read_some(char *buf, size_t len)
+{
+    size_t s = 0;
+    uint32_t cons, prod;
+
+    while ( !test_and_clear_bit(pv_evtchn, shared_info.evtchn_pending) ||
+            (pv_ring->in_cons == pv_ring->in_prod) )
+        hypercall_poll(pv_evtchn);
+
+    cons = pv_ring->in_cons, prod = LOAD_ACQUIRE(&pv_ring->in_prod);
+
+    while ( (s < len) && (0 < (prod - cons)) )
+        buf[s++] = pv_ring->in[cons++ & (sizeof(pv_ring->in) - 1)];
+
+    STORE_RELEASE(&pv_ring->in_cons, cons);
+
+    return s;
+}
+
+/*
  * Write some data into the pv ring, synchronously waiting for all data to be
  * consumed.
  */
diff --git a/include/xtf/console.h b/include/xtf/console.h
index 2a93c06..caec790 100644
--- a/include/xtf/console.h
+++ b/include/xtf/console.h
@@ -25,6 +25,8 @@  void init_pv_console(xencons_interface_t *ring,
 void vprintk(const char *fmt, va_list args) __printf(1, 0);
 void printk(const char *fmt, ...) __printf(1, 2);
 
+size_t pv_console_read_some(char *buf, size_t len);
+
 #endif /* XTF_CONSOLE_H */
 
 /*