USB write delay (windows)

I write to the USB drive from the stream with the lowest priority, using the write in the buffer channel and still from time to time the system as a whole lags behind this operation. If I turn off recording only to disk, everything will be fine. I cannot use calls to Windows APIs, only C write. So I thought, maybe there is a WinAPI function to enable / disable caching of a record on a USB drive, which I could use in combination with FlushBuffers or similar alternatives? The number of drives for undefined operations.

Ideally, I would never lag behind using a write call and caching, if it is done transparently, that's fine too.

EDIT: Will there be a _O_SEQUENTIAL flag for use-only writes?

+6
source share
4 answers

Try lowering the I / O priority for the stream. See this article: http://msdn.microsoft.com/en-us/library/windows/desktop/ms686277(v=vs.85).aspx In particular, use THREAD_MODE_BACKGROUND_BEGIN for your input / output stream. Warning: this does not work in Windows XP

+4
source

The priority of the stream will not affect the delay that occurs during the recording of the media, since it is performed in kernel mode by file systems / disk drivers that do not pay attention to the priority of the calling stream.

You can try using the "T" flag (_O_SHORTLIVED) and flush the buffers at the end of the operation, as well as try to reduce the size of the buffer.

+3
source

There are various types of data transfer for USB, for data there are 3: 1. Bulk Transfer, 2. Isochronous Transfer, and 3. Switch the transfer.

  • Bulk transfers Provides:

     Used to transfer large bursty data. Error detection via CRC, with guarantee of delivery. No guarantee of bandwidth or minimum latency. Stream Pipe - Unidirectional Full & high speed modes only. 

    Bulk transfer is good for data that does not require delivery within a guaranteed time . The USB host controller gives lower priority for mass transfer than other types of transfer.

  • Isochronous transmissions provide:

     Guaranteed access to USB bandwidth. Bounded latency. Stream Pipe - Unidirectional Error detection via CRC, but no retry or guarantee of delivery. Full & high speed modes only. No data toggling. 

    Isochronous transfers occur continuously and periodically. They usually contain time sensitive information, such as an audio or video stream. If there is a delay in the audio stream or a retry of data transmission, then you expect some erratic sound containing glitches. The bit may no longer sync. However, if a packet or frame was deleted from time to time, it is unlikely to be noticed by the listener.

  • Interrupt Transfer Provides:

     Guaranteed Latency Stream Pipe - Unidirectional Error detection and next period retry. 

    Interrupt transfers are usually non-periodic, a small “triggered” device message requires a limited delay. The interrupt request is queued by the device until the host polls the USB device requesting data.

From the above, it seems that you need a guaranteed delay , so you should use Isochronous mode. There are several libraries that can be used as libusb , or you can read more in msdn

+2
source

To find out what makes your system freeze, you first need to deploy the Windows screw. What did Windows do when you experienced a hang?

To find this, you can take a core dump. How to get and analyze a core dump: here .

Depending on the results you get there, you need to decide if there is anything under your control that you can do about. Since you use a third-party library for writing, you can do little more than set the priority of the I / O , the priority of the stream per thread or process level. If you were provided with links to a specific CRT in the library, you could try creating your own custom version, for example. flush after each write to prevent the OS from writing to share only data in large chunks back to disk.

Edit1

It would be best to hide the device after each recording . This can cause the OS to clear any pending data and write the current pending write to disk without caching records to a certain amount.

The second best thing is to just wait after each recording to give the OS the ability to write pending changes, albeit a little back to disk after a certain amount of time.

If you go deeper into performance, you should try XPerf , which has a nice graphical interface and even shows you the call stack your process was hanging in. The Windows Team and many other MS commands use this tool to troubleshoot experience . The latest edition with many features comes with the Windows 8 SDK . But beware that Xperf only works with OS> Vista.

0
source

Source: https://habr.com/ru/post/911800/


All Articles