[Spice-commits] 3 commits - server/red-record-qxl.c server/red-record-qxl.h server/red-worker.c server/reds-private.h server/reds.c server/reds.h

Frediano Ziglio fziglio at kemper.freedesktop.org
Thu Mar 2 16:14:41 UTC 2017


 server/red-record-qxl.c |   35 +++++++++++++++++++++++++++++------
 server/red-record-qxl.h |    3 ++-
 server/red-worker.c     |    8 ++------
 server/reds-private.h   |    2 ++
 server/reds.c           |   18 ++++++++++++++++++
 server/reds.h           |    5 +++++
 6 files changed, 58 insertions(+), 13 deletions(-)

New commits:
commit 0bbfe733b84e57e7543b1b4d09d97db85a6bcaf8
Author: Frediano Ziglio <fziglio at redhat.com>
Date:   Wed Mar 1 22:33:06 2017 +0000

    record: Allocate recording file globally from reds.c
    
    Allows to use recording function for multiple purposes.
    This will allow to register multiple screen VM or recording
    additional stuff like sound.
    
    Signed-off-by: Frediano Ziglio <fziglio at redhat.com>
    Acked-by: Jonathon Jongsma <jjongsma at redhat.com>

diff --git a/server/red-worker.c b/server/red-worker.c
index 8e3530a..c88034b 100644
--- a/server/red-worker.c
+++ b/server/red-worker.c
@@ -1308,7 +1308,6 @@ RedWorker* red_worker_new(QXLInstance *qxl,
     QXLDevInitInfo init_info;
     RedWorker *worker;
     Dispatcher *dispatcher;
-    const char *record_filename;
     RedsState *reds = red_qxl_get_server(qxl->st);
     RedChannel *channel;
 
@@ -1318,10 +1317,7 @@ RedWorker* red_worker_new(QXLInstance *qxl,
     worker->core = event_loop_core;
     worker->core.main_context = g_main_context_new();
 
-    record_filename = getenv("SPICE_WORKER_RECORD_FILENAME");
-    if (record_filename) {
-        worker->record = red_record_new(record_filename);
-    }
+    worker->record = reds_get_record(reds);
     dispatcher = red_qxl_get_dispatcher(qxl);
     dispatcher_set_opaque(dispatcher, worker);
 
diff --git a/server/reds-private.h b/server/reds-private.h
index e7e9ad7..07b7b38 100644
--- a/server/reds-private.h
+++ b/server/reds-private.h
@@ -25,6 +25,7 @@
 #include "main-channel.h"
 #include "inputs-channel.h"
 #include "stat-file.h"
+#include "red-record-qxl.h"
 
 #define MIGRATE_TIMEOUT (MSEC_PER_SEC * 10)
 #define MM_TIME_DELTA 400 /*ms*/
@@ -135,6 +136,7 @@ struct RedsState {
     SpiceCoreInterfaceInternal core;
     GList *qxl_instances;
     MainDispatcher *main_dispatcher;
+    RedRecord *record;
 };
 
 #define FOREACH_QXL_INSTANCE(_reds, _iter, _qxl) \
diff --git a/server/reds.c b/server/reds.c
index f7a32f8..2983aae 100644
--- a/server/reds.c
+++ b/server/reds.c
@@ -3469,7 +3469,9 @@ static const char default_video_codecs[] = "spice:mjpeg;" GSTREAMER_CODECS;
 /* new interface */
 SPICE_GNUC_VISIBLE SpiceServer *spice_server_new(void)
 {
+    const char *record_filename;
     RedsState *reds = spice_new0(RedsState, 1);
+
     reds->config = spice_new0(RedServerConfig, 1);
     reds->config->default_channel_security =
         SPICE_CHANNEL_SECURITY_NONE | SPICE_CHANNEL_SECURITY_SSL;
@@ -3501,6 +3503,12 @@ SPICE_GNUC_VISIBLE SpiceServer *spice_server_new(void)
     reds->listen_socket = -1;
     reds->secure_listen_socket = -1;
 
+    /* This environment was in red-worker so the "WORKER" in it.
+     * For compatibility reason we maintain the old name */
+    record_filename = getenv("SPICE_WORKER_RECORD_FILENAME");
+    if (record_filename) {
+        reds->record = red_record_new(record_filename);
+    }
     return reds;
 }
 
@@ -3709,6 +3717,7 @@ SPICE_GNUC_VISIBLE void spice_server_destroy(SpiceServer *reds)
        close(reds->secure_listen_socket);
     }
 
+    red_record_unref(reds->record);
     reds_cleanup(reds);
 #ifdef RED_STATISTICS
     stat_file_free(reds->stat_file);
@@ -4536,3 +4545,12 @@ static RedCharDeviceVDIPort *red_char_device_vdi_port_new(RedsState *reds)
                         "self-tokens", (guint64)REDS_NUM_INTERNAL_AGENT_MESSAGES,
                         NULL);
 }
+
+RedRecord *reds_get_record(RedsState *reds)
+{
+    if (reds->record) {
+        return red_record_ref(reds->record);
+    }
+
+    return NULL;
+}
diff --git a/server/reds.h b/server/reds.h
index 28e3444..2748102 100644
--- a/server/reds.h
+++ b/server/reds.h
@@ -101,6 +101,11 @@ SpiceCoreInterfaceInternal* reds_get_core_interface(RedsState *reds);
 void reds_update_client_mouse_allowed(RedsState *reds);
 MainDispatcher* reds_get_main_dispatcher(RedsState *reds);
 
+/* Get the recording object stored in RedsState.
+ * You should free with red_record_unref.
+ */
+struct RedRecord *reds_get_record(RedsState *reds);
+
 /* fd watches/timers */
 SpiceWatch *reds_core_watch_add(RedsState *reds,
                                 int fd, int event_mask,
commit ab6ace6b662d83bbd8cdc52bbed542ec83dc4013
Author: Frediano Ziglio <fziglio at redhat.com>
Date:   Wed Mar 1 22:36:26 2017 +0000

    record: Use reference counting for recording
    
    Allows to share the recording object.
    
    Signed-off-by: Frediano Ziglio <fziglio at redhat.com>
    Acked-by: Jonathon Jongsma <jjongsma at redhat.com>

diff --git a/server/red-record-qxl.c b/server/red-record-qxl.c
index be6ac5a..e687c68 100644
--- a/server/red-record-qxl.c
+++ b/server/red-record-qxl.c
@@ -32,6 +32,7 @@ struct RedRecord {
     FILE *fd;
     pthread_mutex_t lock;
     unsigned int counter;
+    gint refs;
 };
 
 #if 0
@@ -910,17 +911,25 @@ RedRecord *red_record_new(const char *filename)
     }
 
     record = g_new(RedRecord, 1);
+    record->refs = 1;
     record->fd = f;
     record->counter = 0;
     pthread_mutex_init(&record->lock, NULL);
     return record;
 }
 
-void red_record_free(RedRecord *record)
+RedRecord *red_record_ref(RedRecord *record)
 {
-    if (record) {
-        fclose(record->fd);
-        pthread_mutex_destroy(&record->lock);
-        g_free(record);
+    g_atomic_int_inc(&record->refs);
+    return record;
+}
+
+void red_record_unref(RedRecord *record)
+{
+    if (!record || !g_atomic_int_dec_and_test(&record->refs)) {
+        return;
     }
+    fclose(record->fd);
+    pthread_mutex_destroy(&record->lock);
+    g_free(record);
 }
diff --git a/server/red-record-qxl.h b/server/red-record-qxl.h
index 0685393..293e24a 100644
--- a/server/red-record-qxl.h
+++ b/server/red-record-qxl.h
@@ -33,7 +33,8 @@ typedef struct RedRecord RedRecord;
  */
 RedRecord* red_record_new(const char *filename);
 
-void red_record_free(RedRecord *record);
+RedRecord *red_record_ref(RedRecord *record);
+void red_record_unref(RedRecord *record);
 
 void red_record_primary_surface_create(RedRecord *record,
                                        QXLDevSurfaceCreate *surface,
diff --git a/server/red-worker.c b/server/red-worker.c
index 250c115..8e3530a 100644
--- a/server/red-worker.c
+++ b/server/red-worker.c
@@ -1457,7 +1457,7 @@ void red_worker_free(RedWorker *worker)
     g_main_context_unref(worker->core.main_context);
 
     if (worker->record) {
-        red_record_free(worker->record);
+        red_record_unref(worker->record);
     }
     memslot_info_destroy(&worker->mem_slots);
     free(worker);
commit e0a5e4f0d082fa4f705ae4881f91b23ba081a960
Author: Frediano Ziglio <fziglio at redhat.com>
Date:   Wed Mar 1 22:27:34 2017 +0000

    record: Synchronize write to record file
    
    The synchronization code is required to avoid mixing writing
    from multiple threads.
    Following patches will add this feature.
    
    Signed-off-by: Frediano Ziglio <fziglio at redhat.com>
    Acked-by: Jonathon Jongsma <jjongsma at redhat.com>

diff --git a/server/red-record-qxl.c b/server/red-record-qxl.c
index ee22236..be6ac5a 100644
--- a/server/red-record-qxl.c
+++ b/server/red-record-qxl.c
@@ -30,6 +30,7 @@
 
 struct RedRecord {
     FILE *fd;
+    pthread_mutex_t lock;
     unsigned int counter;
 };
 
@@ -794,15 +795,17 @@ void red_record_primary_surface_create(RedRecord *record,
 {
     FILE *fd = record->fd;
 
+    pthread_mutex_lock(&record->lock);
     fprintf(fd, "%d %d %d %d\n", surface->width, surface->height,
         surface->stride, surface->format);
     fprintf(fd, "%d %d %d %d\n", surface->position, surface->mouse_mode,
         surface->flags, surface->type);
     write_binary(fd, "data", line_0 ? abs(surface->stride)*surface->height : 0,
         line_0);
+    pthread_mutex_unlock(&record->lock);
 }
 
-void red_record_event(RedRecord *record, int what, uint32_t type)
+static void red_record_event_unlocked(RedRecord *record, int what, uint32_t type)
 {
     red_time_t ts = spice_get_monotonic_time_ns();
     // TODO: record the size of the packet in the header. This would make
@@ -813,12 +816,20 @@ void red_record_event(RedRecord *record, int what, uint32_t type)
     fprintf(record->fd, "event %u %d %u %"PRIu64"\n", record->counter++, what, type, ts);
 }
 
+void red_record_event(RedRecord *record, int what, uint32_t type)
+{
+    pthread_mutex_lock(&record->lock);
+    red_record_event_unlocked(record, what, type);
+    pthread_mutex_unlock(&record->lock);
+}
+
 void red_record_qxl_command(RedRecord *record, RedMemSlotInfo *slots,
                             QXLCommandExt ext_cmd)
 {
     FILE *fd = record->fd;
 
-    red_record_event(record, 0, ext_cmd.cmd.type);
+    pthread_mutex_lock(&record->lock);
+    red_record_event_unlocked(record, 0, ext_cmd.cmd.type);
 
     switch (ext_cmd.cmd.type) {
     case QXL_CMD_DRAW:
@@ -837,6 +848,7 @@ void red_record_qxl_command(RedRecord *record, RedMemSlotInfo *slots,
         red_record_cursor_cmd(fd, slots, ext_cmd.group_id, ext_cmd.cmd.data);
         break;
     }
+    pthread_mutex_unlock(&record->lock);
 }
 
 /**
@@ -900,6 +912,7 @@ RedRecord *red_record_new(const char *filename)
     record = g_new(RedRecord, 1);
     record->fd = f;
     record->counter = 0;
+    pthread_mutex_init(&record->lock, NULL);
     return record;
 }
 
@@ -907,6 +920,7 @@ void red_record_free(RedRecord *record)
 {
     if (record) {
         fclose(record->fd);
+        pthread_mutex_destroy(&record->lock);
         g_free(record);
     }
 }


More information about the Spice-commits mailing list