Use Cache Manager to do your ability (cache Me If you can:using the NT cache manager)

Source: Internet
Author: User

In pediy.com sent a translation, simply reproduced here.

The cache manager is a tightly integrated software component of the NT Memory manager that integrates the file system cache data with the virtual memory system.

The advantage is that the use of physical memory and the file cache and the system run program are balanced for application under the cache manager.

Another key reason for using caching is that files can be accessed by standard file system interfaces, such as read and write. Or form a "memory-mapped" file through the memory manager.

Cache Manager Data Structure: The next segment describes the file system and the cache manager share of the database structures.

Buffer Control bloxk (BCB)

Most of the buffer control block (BCB) is opaque. The first portion of the BCB is exposed to file systems:

typedef struct _PUBLIC_BCB {
CSHORT Nodetypecode;
CSHORT nodebytesize;
ULONG mappedlength;
Large_integer Mappedfileoffset;
} PUBLIC_BCB, *PPUBLIC_BCB;

Buffer control block (BCB)

BCB is used to trace files when this file is mapped to the system address space. The file system is in memory-locked (Pin) data for critical operations. The two fields after the structure are specified for this BCB file management scope.

File Size Information

The file system and Memory Manager each maintain information about the size of the file. Whenever the file system establishes mapping for a file it indicates the current size of the file. Any subsequent changes to the size of the file is similarly indicated to the Cache Manager.

File system and memory management maintain file size information. All subsequent file size changes are also handled by the cache manager display.

There is three values used by the Cache Manager to indicate the current size of the file:

typedef struct _CC_FILE_SIZES {
Large_integer Allocationsize;
Large_integer FileSize;
Large_integer Validdatalength;
} cc_file_sizes, *pcc_file_sizes;

The allocationsize segment is not the physical space actually allocated by the actual file. But the amount of data that is suitable for allocating space.

Allocationsize is the size of the "section object" represented by the memory manager. The section object is used to determine a mapping to a memory file, Allocationsize is often the smallest file size.

Cache Manager Callbacks

Interactions between the file system and the Cache Manager are manipulated via a series ofcallback functions. These callback functions is registered on a per file basis with the cache manager and is then used by the cache manager In order to ensure that the data structures is "locked" prior to performing a file system operation.

The file system and cache Manager are manipulated and interacted through a series of callback functions.

Windows NT assumes there is a strict ordering on how resources are acquired between the file system, Cache Manager, and Me Mory Manager. If followed, this ordering would ensure that deadlocks does not occur. Of course, if it is not followed, deadlocks can (and would) occur. Specifically, file system resources is acquired first. Then the Cache Manager resources are acquired. Finally, Memory Manager Resources is acquired.

Windows NT assumes that in the file system, resource acquisition is in strict order. To ensure that no deadlock occurs. In particular, the file system resource is obtained in the first bit, then the Cache Manager resource, and finally the memory manager resource.

Thus, these callbacks is used by the Cache Manager to honor this hierarchy. The callbacks required by the Cache Manager is:

typedef BOOLEAN (*pacquire_for_lazy_write) (
In PVOID Context,
In BOOLEAN Wait
);

typedef VOID (*prelease_from_lazy_write) (
In PVOID Context
);

typedef BOOLEAN (*pacquire_for_read_ahead) (
In PVOID Context,
In BOOLEAN Wait
);

typedef VOID (*prelease_from_read_ahead) (
In PVOID Context
);

typedef struct _CACHE_MANAGER_CALLBACKS {
Pacquire_for_lazy_write Acquireforlazywrite;
Prelease_from_lazy_write Releasefromlazywrite;
Pacquire_for_read_ahead Acquireforreadahead;
Prelease_from_read_ahead Releasefromreadahead;
} cache_manager_callbacks, *pcache_manager_callbacks;

Notice that the callbacks is used in two different parts of the cache Manager. The first is that lazy writer has the responsibility to write back the dirty cache data to the file system. The second is the pre-read function.

You need to protect non-cached user I/O operations and user-modified file size operations. NT File system uses two EResource structures, from common header to Peresourceresource
Peresource pagingioresource two fields are specified. The Cache manager does not directly fetch resources instead of calling the file system to get the necessary resources.

Static BOOLEAN Owacquireforlazywrite (PVOID Context, BOOLEAN Wait)
{
POW_FCB FCB = (POW_FCB) Context;
BOOLEAN result;

Take out the lock on the file.

result = Owacquireresourceexclusiveexp (&fcb->resource, Wait);
if (!result) {

We did not acquire the resource.

return (result);
}

We did acquire the resource. We need to:
(1) Store away the thread ID of this thread (for the release)
(2) Set top level IRP to a pseudo value
In both cases, the previous value should is zero.

Owassert (!fcb->resourcethread);
Fcb->resourcethread = Owgetcurrentresourcethread ();
return (TRUE);
}

Each of the file systems in the Microsoft IFS kits also contains examples of routines like these. For the Lazy Writer these routines is located in the following locations:

File System

File

Routine

FAT

Resrcsup.c

Fatacquirefcbforlazywrite

CDFS

Resrcsup.c

Cdacquireforcache

RDR2

Rxce\resrcsup.c

Rxacquirefcbforlazywrite

Other similar routines (for read ahead for example) can is located in the same file.

In addition, similar routines such as pre-reading can be found in the same file.

Cccanlwrite

Because an application program can modify data on memory at a rate that exceeds the ability to write the data to disk, the Virtual Memory system can ' fill up ' with data. This in turn can and cause fatal out-of-memory conditions to occur within the VM system. To avoid this, the file system must cooperate with the VM system to detect these conditions. One of the key operations provided by the Cache Manager for thiscccaniwrite. The prototype is:

Applications can modify the rate of memory data beyond write data to disk, and the virtual memory system can be filled with data.

Determines whether data can be written to the cache.

Ntkernelapi BOOLEAN Cccaniwrite (
In Pfile_object FileObject,
In ULONG Bytestowrite,
In BOOLEAN Wait,
In BOOLEAN retrying
);

Use Cache Manager to do your ability (cache Me If you can:using the NT cache manager)

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.