Use of one of the FreeBSD kernel ALQ mechanisms

Source: Internet
Author: User
Tags deprecated

Background:

Because of a project, I have been using FreeBSD to program kernel modules for this period of time. Prior to a period of time for Linux under the driver module programming, Linux module programming is also familiar. Now suddenly transferred to FreeBSD, although both Linux and FreeBSD are Unix-like systems, in the implementation mechanism is still very different, somewhat unaccustomed. The author's project is to write the log information to the disk in a module that is inserted into the kernel . The user layer can be read and written in the form of files, in the kernel module, unlike Linux, users are not recommended to use this method of porting to kernel modules. But FreeBSD provides a ALQ mechanism.

Brief introduction:

alq (asynchronous logging Queues) Asynchronous log queue, which provides an asynchronous, fixed-length and variable-length record mechanism. It can record any vnode. Therefore, the log, character device, and normal file are operations. All function functions accept a struct ALQ implicitly type, maintaining a ALQ state information. This logging mechanism is run as a separate kernel thread, which can be used for the system all log request services. every ale ( Asynchronous log entry) is defined as a struct ale

   struct Ale {   intptr_t   ae_bytesused;   /* #bytes written to ALE. */   char   *ae_data;   /* Write ptr. */   int   ae_pad;   /* Unused, Compat. */   };
One ALQCan be created in two modes, fixed length or length. Variable-length ALQAdapt Alq_writen ()And Alq_getn ()The length of the write when used. The fixed-length ALQ is already specified when the queue is created. The fixed-length mode is now is deprecated, you can replace it with a variable mode.

Function:

     #include <sys/alq.h>
 int alq_open_flags(struct ALQ **app, const char *file, struct ucred *cred<   /c9>, int cmode, int size, int flags); Create a variable-length ALQ.  fileas the file name. If the file does not exist, Alq_open () tries to create it, andCmodeThe parameter is passed to Alq_open () as the property that created the file. For most users, this cmdode should be designated as
     Alq_default_cmode. Thecred parameter specifies the trust certificate when the file is opened and the I/O operation is performed. Size to set the queue (in bytes). The last parameter flag is usually set    to alq_ordered to       indicate that the order in which the write thread waits for busy ALQ to release resources should be preserved. Successful return 0, otherwise an error code (open (2)) is returned.
     intalq_open(struct ALQ **app,const char *file,struct ucred *cred int Cmode int size int count); The deprecated Alq_open () is encapsulated in Alq_open_flags (), in order to be compatible with the older version. All         parameters are passed indirectly through Alq_open_flags (). Specific I do not introduce here, interested readers can refer to Alq. 
    intalq_writen(struct ALQ *alqvoid *dataint lenint flags) ; writes Len bytes of data to the ALQ in the Set variable-length mode from data. If Alq_writ    e () does not immediately write  into this data, and the flags parameter is     set to Alq_waitok, it will use the Msleep_spin (9) Sleep wait data. A write operation automatically dispatches the queue Alq to     write to the disk. This behavior can be controlled by passing Alq_noacti vate to the flags parameter     to control that it will not be written to disk. Returns Ewouldblock if the alq_nowait is set or the queue is full or the system shuts down. 
     int Alq_write (struct ALQ *alqvoid *dataint flags); the discarded alq_write () is enclosed in alq_writen (), and interested readers can refer to Alq  . 
    void Alq_flush (struct ALQ *alq); refreshes alq to log file. If the ALQ has data that can be refreshed and has not been refreshed, it will block and go for IO operations. Otherwise, return immediately.
   voidalq_close(struct ALQ *alq); Turns off Alq, refreshes all pending write requests to the log file. Frees all resources for the request.
  struct ale *    alq_getn   struct alq *alq   int len   int flags   alq_po is called. If ALQ_GETN () cannot get the Len length byte data immediately, and Alq_waitok is set in flags, it will block the wait data. The caller can set the   in the struct ale in advance;  
        ae_bytesused field, select to return less than Len bytes into the ale. Returns null if the alq_nowait is set or the queue is full or the system shuts down.
    Alq_get (struct ALQ *alqint flags); is deprecated and encapsulated in ALQ_GETN ().
    Alq_post_flags (struct ALQ *alqstruct ale *aleint flags); The dispatch ale (obtained from ALQ_GETN () or Alq_get ()) is used to write Alq. You can set alq_noactivate in the flags parameter
    Indicates that the queue is not immediately flushed to disk. The ALQ will be unlocked.
Reference documents:
    Alq

Use of one of the FreeBSD kernel ALQ mechanisms

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.