events req servicing v2
[lttv.git] / ltt / branches / poly / doc / developer / lttvwindow_events_delivery.txt
index 2a7da4e0d6af010c10c3c63afa3bd349451b40ae..30678ae24e72a0236e67ef139444fea69b53c798 100644 (file)
@@ -60,14 +60,17 @@ has.
 - Border effects
 
 Viewer's writers will have to deal with a lot of border effects caused by the
-particularities of the reading by selecting the information.
+particularities of the reading. They will be required to select the information
+they need from their input by filtering.
 
-- Lack of encapsulation
+- Lack of encapsulation and difficulty of testing
 
 The viewer's writer will have to take into account all the border effects caused
 by the interaction with other modules. This means that event if a viewer works
 well alone or with another viewer, it's possible that new bugs arises when a new
-viewer comes around.
+viewer comes around. So, even if a perfect testbench works well for a viewer, it
+does not confirm that no new bug will arise when another viewer is loaded at the
+same moment asking for different time intervals.
 
 
 - Duplication of the work
@@ -101,7 +104,7 @@ Strengths
 Having the opportunity of delivering more events than necessary to the viewers
 means that we can concatenate time intervals and number of events requested
 fairly easily, while being hard to determine if some specific cases will be
-wrong by formal methods.
+wrong, in depth testing being impossible.
 
 - No duplication of the tracecontext API
 
@@ -117,10 +120,6 @@ a layer of encapsulation.
 The idea behind this method is to provide exactly the events requested by the
 viewers to them, no more, no less.
 
-This method relies on the fact that time based and number based event requests
-are, by nature, totally different and that there is no real interest in merging
-both requests types.
-
 It uses the new API for process traceset suggested in the document
 process_traceset_strict_boundaries.txt.
 
@@ -132,8 +131,8 @@ removing hooks for the different time intervals requested. That means that hooks
 insertion and removal will be done between each traceset processing based on
 the time intervals and event positions related to each hook. We must therefore
 provide a simple interface for hooks passing between the viewers and the main
-window, make them easier to manage from the main window. The new type
-LttvHooksPrio solves this problem.
+window, make them easier to manage from the main window. A modification to the
+LttvHooks type solves this problem.
 
 
 Architecture
@@ -141,21 +140,9 @@ Architecture
 Added to the lttvwindow API :
 
 
-- lttvwindow_time_interval_request
-arguments :
-( MainWindow *main_win,
-  TimeWindow time_requested, guint num_events,
-  LttvHooksPrio process_traceset_middle,
-  LttvHook after_process_traceset,
-  gpointer after_process_traceset_data);
-
-- lttvwindow_position_request
-arguments :
-( MainWindow *main_win,
-  LttvTracesetPosition position, guint max_num_events,
-  LttvHooksPrio process_traceset_middle,
-  LttvHook after_process_traceset,
-  gpointer after_process_traceset_data);
+void lttvwindow_events_request
+( MainWindow                  *main_win,
+  EventsRequest               *events_request);
 
 
 Internal functions :
@@ -167,91 +154,146 @@ Internal functions :
 Implementation
 
 
-- Type LttvHooksPrio
-
-see hook_prio.h
-
-
-- lttvwindow_time_interval_request
-
-It adds the TimeRequest struct to the array of time requests pending and
-registers a pending request for the next g_idle if none is registered.
+- Type LttvHooks
 
-typedef struct _TimeRequest {
-  TimeWindow  time_window;
-  guint num_events;
-  LttvHooksPrio middle_hooks;
-  LttvHook after_hook;
-  gpointer after_hook_data;
-} TimeRequest;
+see hook_prio.txt
 
+The viewers will just have to pass hooks to the main window through this type,
+using the hook.h interface to manipulate it. Then, the main window will add
+them and remove them from the context to deliver exactly the events requested by
+each viewer through process traceset.
 
-- lttvwindow_position_request
 
-It adds a PositionRequest struct to the array of position requests pending and
-registers a pending request for the next g_idle if none is registered.
+- lttvwindow_events_request
 
-typedef struct _PositionRequest {
-  LttvTracesetPosition  position;
-  guint max_num_events;
-  LttvHooksPrio middle_hooks;
-  LttvHook after_hook;
-  gpointer after_hook_data;
-} PositionRequest;
+It adds the an EventsRequest struct to the array of time requests
+pending and registers a pending request for the next g_idle if none is
+registered. The viewer can access this structure during the read as its
+hook_data. Only the stop_flag can be changed by the viewer through the
+event hooks.
 
+typedef struct _EventsRequest {
+  gpointer                    viewer_data;
+  LttTime                     start_time,       /* Unset : { 0, 0 }         */
+  LttvTracesetContextPosition start_position,   /* Unset : num_traces = 0   */
+  gboolean                    stop_flag,        /* Continue:TRUE Stop:FALSE */
+  LttTime                     end_time,         /* Unset : { 0, 0 }         */
+  guint                       num_events,       /* Unset : G_MAXUINT        */
+  LttvTracesetContextPosition end_position,     /* Unset : num_traces = 0   */
+  LttvHooks                  *before_traceset,  /* Unset : NULL             */
+  LttvHooks                  *before_trace,     /* Unset : NULL             */
+  LttvHooks                  *before_tracefile, /* Unset : NULL             */
+  LttvHooks                  *event,            /* Unset : NULL             */
+  LttvHooksById              *event_by_id,      /* Unset : NULL             */
+  LttvHooks                  *after_tracefile,  /* Unset : NULL             */
+  LttvHooks                  *after_trace,      /* Unset : NULL             */
+  LttvHooks                  *after_traceset    /* Unset : NULL             */
+} EventsRequest;
 
 
 - lttvwindow_process_pending_requests
 
 This internal function gets called by g_idle, taking care of the pending
 requests. It is responsible for concatenation of time intervals and position
-requests. It does it while it calls process traceset. Here is the detailed
-description of the way it works :
-
-It treats time interval requests and position requests as two different cases.
-So let's start with time interval requests.
-
-- Time interval requests servicing
-
-(1)
-It starts by finding the time interval request with the lowest start time and
-the others with the same start time. It add its (or their) hooks to the context.
-It will use this start time to seek in the traceset.
-
-Then, it searches for what event comes first : the end of one of the time
-request actually added in the context or the start of a time request that is not
-in the context. It uses this value as a end boundary for the first process
-traceset middle call.
-
-After a process traceset middle ends, we check if we have reached the end time
-of any time request. If so, we call the time requests process traceset end hook
-and remove this time request from the context and the array of time requests. If
-the context has no hooks left, that means that we have to jump further in the
-traceset. We then simply have to use the exact routine that we used for (1).
-
-Else, if there are hooks left, that means that we have not finished requesting
-one hook's time interval request yet, but maybe we must add a new time request
-to the hook list of the context. We start back at point (1), except that instead
-of finding the lowest start time, we simply keep the hooks already present in
-the context and add hooks that has their start time with a value equal to the
-last process traceset's end time.
-
-
-- Position requests servicing
-
-As it is nearly impossible to compare two traceset positions without replaying
-part of the traceset reading, which is not very efficient, we consider that the
-performance cost of doing one trace read per request does not justify position
-requests combinations. So, each position request will be serviced independently.
-
+requests. It does it with the following algorithm organizing process traceset
+calls. Here is the detailed description of the way it works :
+
+
+- Events Requests Servicing Algorithm
+
+Data structures necessary :
+
+List of requests added to context : list_in
+List of requests not added to context : list_out
+
+Initial state :
+
+list_in : empty
+list_out : many events requests
+
+
+While list_in !empty and list_out !empty
+  1. If list_in is empty (need a seek)
+    1.1 Add requests to list_in
+      1.1.1 Find all time requests with the lowest start time in list_out 
+            (ltime)
+      1.1.2 Find all position requests with the lowest position in list_out
+            (lpos)
+      1.1.3 If lpos.start time < ltime
+        - Add lpos to list_in, remove them from list_out
+      1.1.4 Else, (lpos.start time >= ltime)
+        - Add ltime to list_in, remove them from list_out
+    1.2 Seek
+      1.2.1 If first request in list_in is a time request
+        1.2.1.1 Seek to that time
+      1.2.2 Else, the first request in list_in is a position request
+        1.2.2.1 Seek to that position
+    1.3 Call begin for all list_in members
+      (1.3.1 begin hooks called)
+      (1.3.2 middle hooks added)
+  2. Else, list_in is not empty, we continue a read
+    2.1 For each req of list_out
+    - if req.start time == current context time
+      - Add to list_in, remove from list_out
+      - Call begin
+    - if req.start position == current position
+      - Add to list_in, remove from list_out
+      - Call begin
+
+  3. Find end criterions
+    3.1 End time
+      3.1.1 Find lowest end time in list_in
+      3.1.2 Find lowest start time in list_out
+      3.1.3 Use lowest of both as end time
+    3.2 Number of events
+      3.2.1 Find lowest number of events in list_in
+    3.3 End position
+      3.3.1 Find lowest end position in list_in
+      3.3.2 Find lowest start position in list_out
+      3.3.3 Use lowest of both as end position
+
+  4. Call process traceset middle
+    4.1 Call process traceset middle (Use end criterion found in 3)
+      * note : end criterion can also be viewer's hook returning TRUE
+  5. After process traceset middle
+    - if current context time > traceset.end time
+      - For each req in list_in
+        - Call end for req
+        - remove req from list_in
+    5.1 For each req in list_in
+          - req.num -= count
+          - if req.num == 0
+            - Call end for req
+            - remove req from list_in
+          - if current context time > req.end time
+            - Call end for req
+            - remove req from list_in
+          - if req.end pos == current pos
+            - Call end for req
+            - remove req from list_in
+          - if req.stop_flag == TRUE
+            - Call end for req
+            - remove req from list_in
+
+
+
+Notes :
+End criterions for process traceset middle :
+If the criterion is reached, event is out of boundaries and we return.
+Current time >= End time
+Event count > Number of events
+Current position >= End position
+Last hook list called returned TRUE
+
+The >= for position is necessary to make ensure consistency between start time
+requests and positions requests that happens to be at the exact same start time
+and position.
 
 
 
 Weaknesses
 
-- Position requests are serviced independently, which may duplicate traceset
-reads.
-
+- None (nearly?) :)
 
 
 Strengths
@@ -262,3 +304,184 @@ Strengths
 
 - Solves all the weaknesses idenfied in the actual boundaryless traceset
 reading.
+
+
+
+
+- Revised Events Requests Servicing Algorithm (v2)
+
+typedef LttvEventsRequestPrio guint;
+
+typedef struct _EventsRequest {
+  gpointer                     viewer_data;
+  gboolean                     servicing;       /* service in progress: TRUE */ 
+  LttvEventsRequestPrio        prio;            /* Ev. Req. priority        */
+  LttTime                      start_time;      /* Unset : { 0, 0 }         */
+  LttvTracesetContextPosition *start_position;  /* Unset : num_traces = 0   */
+  gboolean                     stop_flag;       /* Continue:TRUE Stop:FALSE */
+  LttTime                      end_time;        /* Unset : { 0, 0 }         */
+  guint                        num_events;      /* Unset : G_MAXUINT        */
+  LttvTracesetContextPosition *end_position;    /* Unset : num_traces = 0   */
+  LttvHooks                   *before_traceset; /* Unset : NULL             */
+  LttvHooks                   *before_trace;    /* Unset : NULL             */
+  LttvHooks                   *before_tracefile;/* Unset : NULL             */
+  LttvHooks                   *event;           /* Unset : NULL             */
+  LttvHooksById               *event_by_id;     /* Unset : NULL             */
+  LttvHooks                   *after_tracefile; /* Unset : NULL             */
+  LttvHooks                   *after_trace;     /* Unset : NULL             */
+  LttvHooks                   *after_traceset;  /* Unset : NULL             */
+  LttvHooks                   *before_chunk;    /* Unset : NULL             */
+  LttvHooks                   *after_chunk      /* Unset : NULL             */
+} EventsRequest;
+
+
+The reads are splitted in chunks. After a chunk is over, we want to check if
+there is a GTK Event pending and execute it. It can add or remove events 
+requests from the event requests list. If it happens, we want to start over
+the algorithm from the beginning.
+
+Two levels of priority exists. High priority and low priority. High prio
+requests are serviced first, even if lower priority requests has lower start
+time or position.
+
+
+Data structures necessary :
+
+List of requests added to context : list_in
+List of requests not added to context : list_out
+
+Initial state :
+
+list_in : empty
+list_out : many events requests
+
+
+A. While list_in !empty and list_out !empty and !GTK Event pending
+  1. If list_in is empty (need a seek)
+    1.1 Add requests to list_in
+      1.1.1 Find all time requests with the highest priority and lowest start
+            time in list_out (ltime)
+      1.1.2 Find all position requests with the highest priority and lowest
+            position in list_out (lpos)
+      1.1.3 If lpos.prio > ltime.prio 
+                  || (lpos.prio == ltime.prio && lpos.start time < ltime)
+        - Add lpos to list_in, remove them from list_out
+      1.1.4 Else, (lpos.prio < ltime.prio 
+                        ||(lpos.prio == ltime.prio && lpos.start time >= ltime))
+        - Add ltime to list_in, remove them from list_out
+    1.2 Seek
+      1.2.1 If first request in list_in is a time request
+        1.2.1.1 Seek to that time
+      1.2.2 Else, the first request in list_in is a position request
+        1.2.2.1 If the position is the same than the saved state, restore state
+        1.2.2.1 Else, seek to that position
+    1.3 Add hooks and call begin for all list_in members
+      1.3.1 If !servicing
+          - begin hooks called
+          - servicing = TRUE
+      1.3.2 call before_chunk
+      1.3.3 events hooks added
+  2. Else, list_in is not empty, we continue a read
+    2.1 For each req of list_out
+    - if req.start time == current context time
+      - Add to list_in, remove from list_out
+      - If !servicing
+        - Call begin
+        - servicing = TRUE
+      - Call before_chunk
+      - events hooks added
+    - if req.start position == current position
+      - Add to list_in, remove from list_out
+      - If !servicing
+        - Call begin
+        - servicing = TRUE
+      - Call before_chunk
+      - events hooks added
+
+  3. Find end criterions
+    3.1 End time
+      3.1.1 Find lowest end time in list_in
+      3.1.2 Find lowest start time in list_out (>= than current time*)
+                                * To eliminate lower prio requests
+      3.1.3 Use lowest of both as end time
+    3.2 Number of events
+      3.2.1 Find lowest number of events in list_in
+      3.2.2 Use min(CHUNK_NUM_EVENTS, min num events in list_in) as num_events
+    3.3 End position
+      3.3.1 Find lowest end position in list_in
+      3.3.2 Find lowest start position in list_out (>= than current
+                                                    position)
+      3.3.3 Use lowest of both as end position
+
+  4. Call process traceset middle
+    4.1 Call process traceset middle (Use end criterion found in 3)
+      * note : end criterion can also be viewer's hook returning TRUE
+  5. After process traceset middle
+    - if current context time > traceset.end time
+      - For each req in list_in
+        - Call end for req
+        - Remove events hooks for req
+        - remove req from list_in
+    5.1 For each req in list_in
+          - req.num -= count
+          - if req.num == 0
+            - Call end for req
+            - Remove events hooks for req
+            - remove req from list_in
+          - if current context time > req.end time
+            - Call end for req
+            - Remove events hooks for req
+            - remove req from list_in
+          - if req.end pos == current pos
+            - Call end for req
+            - Remove events hooks for req
+            - remove req from list_in
+          - if req.stop_flag == TRUE
+            - Call end for req
+            - Remove events hooks for req
+            - remove req from list_in
+          - if exists one events requests in list_out that has
+                  higher priority and time != current time
+            - Use current position as start position for req
+            - Remove start time from req
+            - Call after_chunk for req
+            - Remove event hooks for req
+            - Put req back in list_out, remove from list_in
+            - Save current state into saved_state.
+
+B. When interrupted
+  1. for each request in list_in
+    1.1. Use current postition as start position
+    1.2. Remove start time
+    1.3. Call after_chunk
+    1.4. Remove event hooks
+    1.5. Put it back in list_out
+  2. Save current state into saved_state.
+    2.1 Free old saved state.
+    2.2 save current state.
+
+
+
+
+
+Notes :
+End criterions for process traceset middle :
+If the criterion is reached, event is out of boundaries and we return.
+Current time >= End time
+Event count > Number of events
+Current position >= End position
+Last hook list called returned TRUE
+
+The >= for position is necessary to make ensure consistency between start time
+requests and positions requests that happens to be at the exact same start time
+and position.
+
+We only keep one saved state in memory. If, for example, a low priority
+servicing is interrupted, a high priority is serviced, then the low priority
+will use the saved state to start back where it was instead of seeking to the
+time. In the very specific case where a low priority servicing is interrupted,
+and then a high priority servicing on top of it is also interrupted, well, the
+low priority will loose its state and will have to seek back. It should not
+occur often. The solution to it would be to save one state per priority.
+
+
This page took 0.027865 seconds and 4 git commands to generate.