21 Commits

Author SHA1 Message Date
Johan Wikman
402b27ad01 MXS-1392 Remove remnats of DCB reference counting 2017-09-08 12:41:41 +03:00
Johan Wikman
be94066b77 MXS-1392 Manage DCB lifetime using refcounts
Now it is also possible to ensure that a DCB stays alive while
a task referring to it is posted from one worker to another.
That will be implemented in a subsequent commit.
2017-09-08 12:41:41 +03:00
Johan Wikman
7e17e2cd56 MXS-1392 Add reference count to MXS_POLL_DATA
The polling mechanism can now optionally be used for managing
the lifetime of an object placed into the poll set.

If a MXS_POLL_DATA has a non-null 'free', then the reference count
of the data will be increased before calling the handler and
decreased after. In that case, if the reference count reaches 0,
the free function will be called.

Note that the reference counts of *all* MXS_POLL_DATAs returned
by 'epoll_wait' will be increased before the events are delivered
to the handlers individually for each MXS_POLL_DATA, and then once
all events have been delivered, the reference count of each
MXS_POLL_DATA will be decreased.

This ensure that if there are interdependencies between different
MXS_POLL_DATAs returned by one call to 'epoll_wait', the case that
an MXS_POLL_DATA is deleted before its events have been delivered
can be avoided by using the reference count for lifetime management.

In subsequent commits, the reference count will be taken into use
in the lifetime management of DCBs.
2017-09-08 12:41:41 +03:00
Markus Mäkelä
92fa613f93 Use worker messages for fake events
Fake events now use worker messages to trigger execution of the events.
2017-06-07 10:36:26 +03:00
Johan Wikman
f546a17e77 Update change date of 2.2 2017-06-01 10:24:20 +03:00
Johan Wikman
db3153ee4e Move statistics to Worker
Now the statistics is in a single structure and the property of the
Worker instance in question. Methods are provided for obtaining the
statistics of all workers in one go.
2017-04-20 13:51:16 +03:00
Johan Wikman
722d6da46f The thread state is now a property of the worker
A worker's state is not statistics, but transient information.
2017-04-20 13:51:16 +03:00
Johan Wikman
effa2f5674 poll_waitevents moved to Worker
A direct move without any non-essential modifications. Poll_waitevents will
be turned into a regular methods using instance variables.
2017-04-20 13:51:16 +03:00
Johan Wikman
3ac619bfec Fold thread state into poll stats 2017-04-20 13:51:16 +03:00
Johan Wikman
30d7f52852 Remove meaningsless stats
Showing dcb addresses, the number of fds and the events is
meaningless as the information is completely transient and
is likely to have changed the moment is was displayed.
2017-04-20 13:51:16 +03:00
Johan Wikman
05d9d31499 Remove meaningless statistics
Counting how many times no threads are in epoll_wait does not
make sense anymore, now that threads do not share any work.
2017-04-20 13:51:16 +03:00
Johan Wikman
f952a11eb8 Move queue statistics to Worker
Just like the thread stats and poll stats earlier, the queue stats
are now moved to worker.

A litte refactoring still, and the polling will only work on local
data.
2017-04-20 13:51:16 +03:00
Johan Wikman
76825eb2c5 Poll statistics moved to worker
Each worker now has a separate structure for collecting the
polling statistics that is passed to epoll_waitevents(). When
the stats are asked for, we loop over all separate stats and
combine them. So, instead of having every statistics of each
thread one cacheline apart, each thread has all its statistics
in one lump that, for obvious reasons, are going to be apart.

The primary purpose of this excersize is to remove the hardwired
nature of the statistics collection. For instance, the admin
thread will be doing I/O but that I/O should not be included
in the statistics of the workers.
2017-04-20 13:51:16 +03:00
Johan Wikman
c11ca1c328 Move thread data to workers
This is a step in the direction that any worker/thread related data
is the property of the worker/thread.
2017-04-20 13:51:16 +03:00
Johan Wikman
b8c78a23df Add MessageQueue class
MessageQueue encapsulates a message queue built on top of a
pipe. The message queue needs a handler for receiving messages
and must be added to a worker for pumping messages through the
pipe.

Each Worker will have an instance of MessageQueue.
2017-04-20 13:51:16 +03:00
Johan Wikman
21ac606ee1 poll_[add|remove]_fd_[from|to]_worker made boolean 2017-04-20 13:51:16 +03:00
Johan Wikman
64ab48f698 Sorting out rebase 2017-04-20 13:51:16 +03:00
Johan Wikman
bb6e0767cc Add first version of MXS_WORKER
MXS_WORKER is an abstraction of a worker aka worker thread.
It has a pipe whose read descriptor is added to the worker/thread
specific poll set and a write descriptor used for sending messages
to the worker.

The worker exposes a function mxs_worker_post_message using which
messages can be sent to the worker. These messages can be sent from
any thread but will be delivered on the thread dedicated for the
worker.

To illustrate how it works, maxadmin has been provided with a new
command "ping workers" that sends a message to every worker, which
then logs a message to the log.

Additional refactoring are needed, since there currently are overlaps
and undesirable interactions between the poll mechanism, the thread
mechanism and the worker mechanism.

This is visible currently, for instance, by it not being possible to
shut down MaxScale. The reason is that the workers should be shut down
first, then the poll mechanism and finally the threads. The shutdown
need to be arranged so that a shutdown message is sent to the workers
who then cause the polling loop to exit, which will cause the threads
to exit.

That can be arranged cleanly by making poll_waitevents() a "method"
of the worker, which implies that the poll set becomes a "member
variable" of the worker.

To be continued.
2017-04-20 13:51:16 +03:00
Johan Wikman
22b39daf06 Make EPOLLET the default
The whole worker thread mechanism assumes EPOLLET and non-blocking
descriptors, so that should be the default.

TODO: In debug mode, check that the provided file descriptor indeed
      is non-blocking.
2017-04-20 13:51:16 +03:00
Johan Wikman
b11b848e66 Move all statistics gathering to poll_waitevents().
The handler callback should now return a bitmask with bits set
according to what it did when it was called. That way the actual
statistics gathering can be done in poll_waitevents() and the
handler need not be aware of any thread structs.

Actually, the only thing that needs any assistance is accept handling,
because in poll_waitevents() we do not know whether a READ event
relates to a listening or a normal socket, that is, should the
event be counted as an accept or as a read.
2017-04-20 13:51:16 +03:00
Johan Wikman
63141bb191 WIP: Allow the adding of any fds to the poll set
This is just a first step in a trial that will allow the addition
of any file descriptor to the general poll mechanism and hence
allow any i/o to be handled by the worker threads.

There is a structure

  typedef struct mxs_poll_data
  {
      void (*handler)(struct mxs_poll_data *data, int wid, uint32_t events);
      struct
      {
          int id;
      } thread;
  } MXS_POLL_DATA;

that any other structure (e.g. a DCB) encapsulating a file descriptor must
have as its first member (a C++ struct could basically derive from it).

That structure contains two members; 'handler' and 'thread.id'. Handler is a
pointer to a function taking a pointer to a struct mxs_poll_data, a worker thread
if and an epoll event mask as argument.

So, DCB is modified to have MXS_POLL_DATA as its first member and 'handler'
is initialized with a function that *knows* the passed MXS_POLL_DATA can
be downcast to a DCB.

process_pollq no longer exists, but is now called process_pollq_dcb. The
general stuff related to statistics etc. will be moved to poll_waitevents
itself after which the whole function is moved to dcb.c. At that point,
the handler pointer will be set in dcb_alloc().

Effectively poll.[h|c] will provide a generic mechanism for listening on
whatever descriptors and the dcb stuff will be part of dcb.[h|c].
2017-04-20 13:51:16 +03:00