本文整理汇总了C++中posix_fadvise函数的典型用法代码示例。如果您正苦于以下问题:C++ posix_fadvise函数的具体用法?C++ posix_fadvise怎么用?C++ posix_fadvise使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了posix_fadvise函数的20个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于我们的系统推荐出更棒的C++代码示例。
示例1: vfileLogger
//.........这里部分代码省略.........
const time_t elapsedTime = currentTime - startTime;
if (unlikely(elapsedTime >= maxDuration)) {
close(logFile);
logFile = -1;
}
} /* (LOG_FILE_DURATION & LogStat) */
if (unlikely(-1 == logFile)) {
error = createFile();
/* error already logged */
}
if (likely(EXIT_SUCCESS == error)) {
const ssize_t written = write(logFile,buf,n);
if (written > 0) {
if (unlikely(written != n)) {
ERROR_MSG("only %d byte(s) of %d has been written to %s",written,n,fullFileName);
if (LogStat & LOG_CONS) {
int fd = open("/dev/console", O_WRONLY | O_NOCTTY, 0);
if (fd >= 0 ) {
dprintf(fd,"logMsg");
close(fd);
fd = -1;
}
} /* (LogStat & LOG_CONS) */
if (unlikely((LogStat & LOG_FILE_SYNC_ON_ERRORS_ONLY) && (LOG_PRI(priority) <= LOG_ERR))) {
/* flush data if the log priority is "upper" or equal to error */
error = fdatasync(logFile);
if (error != 0) {
error = errno;
ERROR_MSG("fdatasync to %s error %d (%m)",fullFileName,error);
}
error = posix_fadvise(logFile, 0,0,POSIX_FADV_DONTNEED); /* tell the OS that log message bytes could be released from the file system cache */
if (error != 0) {
ERROR_MSG("posix_fadvise to %s error %d (%m)",fullFileName,error);
}
} /* (unlikely((LogStat & LOG_FILE_SYNC_ON_ERRORS_ONLY) && (LOG_PRI(priority) <= LOG_ERR))) */
} /* (unlikely(written != n)) */
fileSize += written;
#ifdef FILESYSTEM_PAGE_CACHE_FLUSH_THRESHOLD
static size_t currentPageCacheMaxSize = 0;
currentPageCacheMaxSize += written;
if (currentPageCacheMaxSize >= FILESYSTEM_PAGE_CACHE_FLUSH_THRESHOLD) {
/* tell the OS that log message bytes could be released from the file system cache */
if (likely(posix_fadvise(logFile, 0,0,POSIX_FADV_DONTNEED) == 0)) {
currentPageCacheMaxSize = 0;
DEBUG_MSG("used file system cache allowed to be flushed (size was %u)",currentPageCacheMaxSize);
} else {
NOTICE_MSG("posix_fadvise to %s error %d (%m), current page cache max size is %u",fullFileName,error,currentPageCacheMaxSize);
}
}
#endif /* FILESYSTEM_PAGE_CACHE_FLUSH_THRESHOLD */
if ((LOG_FILE_ROTATE & LogStat) || (LOG_FILE_HISTO & LogStat)) {
if (fileSize >= maxSize) {
close(logFile);
logFile = -1;
}
}
} else if (0 == written) {
WARNING_MSG("nothing has been written in %s", fullFileName);
} else {
error = errno;
ERROR_MSG("write to %s error %d (%m)", fullFileName, error);
开发者ID:Oliviers-OSS,项目名称:dbgflags,代码行数:67,代码来源:fileLogger.c
示例2: __wt_bm_read
/*
* __wt_bm_read --
* Map or read address cookie referenced block into a buffer.
*/
int
__wt_bm_read(WT_BM *bm, WT_SESSION_IMPL *session,
WT_ITEM *buf, const uint8_t *addr, size_t addr_size)
{
WT_BLOCK *block;
int mapped;
off_t offset;
uint32_t cksum, size;
WT_UNUSED(addr_size);
block = bm->block;
/* Crack the cookie. */
WT_RET(__wt_block_buffer_to_addr(block, addr, &offset, &size, &cksum));
/*
* Map the block if it's possible.
*/
mapped = bm->map != NULL && offset + size <= (off_t)bm->maplen;
if (mapped) {
buf->data = (uint8_t *)bm->map + offset;
buf->size = size;
WT_RET(__wt_mmap_preload(session, buf->data, buf->size));
WT_STAT_FAST_CONN_INCR(session, block_map_read);
WT_STAT_FAST_CONN_INCRV(session, block_byte_map_read, size);
return (0);
}
#ifdef HAVE_DIAGNOSTIC
/*
* In diagnostic mode, verify the block we're about to read isn't on
* the available list, or for live systems, the discard list.
*
* Don't check during the salvage read phase, we might be reading an
* already freed overflow page.
*/
if (!F_ISSET(session, WT_SESSION_SALVAGE_CORRUPT_OK))
WT_RET(__wt_block_misplaced(
session, block, "read", offset, size, bm->is_live));
#endif
/* Read the block. */
WT_RET(__wt_block_read_off(session, block, buf, offset, size, cksum));
#ifdef HAVE_POSIX_FADVISE
/* Optionally discard blocks from the system's buffer cache. */
if (block->os_cache_max != 0 &&
(block->os_cache += size) > block->os_cache_max) {
WT_DECL_RET;
block->os_cache = 0;
/* Ignore EINVAL - some file systems don't support the flag. */
if ((ret = posix_fadvise(block->fh->fd,
(off_t)0, (off_t)0, POSIX_FADV_DONTNEED)) != 0 &&
ret != EINVAL)
WT_RET_MSG(
session, ret, "%s: posix_fadvise", block->name);
}
#endif
return (0);
}
开发者ID:EaseTech,项目名称:wiredtiger,代码行数:65,代码来源:block_read.c
示例3: malloc
static struct btrfs_fs_info *__open_ctree_fd(int fp, const char *path,
u64 sb_bytenr,
u64 root_tree_bytenr, int writes,
int partial)
{
u32 sectorsize;
u32 nodesize;
u32 leafsize;
u32 blocksize;
u32 stripesize;
u64 generation;
struct btrfs_key key;
struct btrfs_root *tree_root = malloc(sizeof(struct btrfs_root));
struct btrfs_root *extent_root = malloc(sizeof(struct btrfs_root));
struct btrfs_root *chunk_root = malloc(sizeof(struct btrfs_root));
struct btrfs_root *dev_root = malloc(sizeof(struct btrfs_root));
struct btrfs_root *csum_root = malloc(sizeof(struct btrfs_root));
struct btrfs_fs_info *fs_info = malloc(sizeof(*fs_info));
int ret;
struct btrfs_super_block *disk_super;
struct btrfs_fs_devices *fs_devices = NULL;
u64 total_devs;
u64 features;
if (sb_bytenr == 0)
sb_bytenr = BTRFS_SUPER_INFO_OFFSET;
/* try to drop all the caches */
if (posix_fadvise(fp, 0, 0, POSIX_FADV_DONTNEED))
fprintf(stderr, "Warning, could not drop caches\n");
ret = btrfs_scan_one_device(fp, path, &fs_devices,
&total_devs, sb_bytenr);
if (ret) {
fprintf(stderr, "No valid Btrfs found on %s\n", path);
goto out;
}
if (total_devs != 1) {
ret = btrfs_scan_for_fsid(fs_devices, total_devs, 1);
if (ret)
goto out;
}
memset(fs_info, 0, sizeof(*fs_info));
fs_info->super_copy = calloc(1, BTRFS_SUPER_INFO_SIZE);
fs_info->tree_root = tree_root;
fs_info->extent_root = extent_root;
fs_info->chunk_root = chunk_root;
fs_info->dev_root = dev_root;
fs_info->csum_root = csum_root;
if (!writes)
fs_info->readonly = 1;
extent_io_tree_init(&fs_info->extent_cache);
extent_io_tree_init(&fs_info->free_space_cache);
extent_io_tree_init(&fs_info->block_group_cache);
extent_io_tree_init(&fs_info->pinned_extents);
extent_io_tree_init(&fs_info->pending_del);
extent_io_tree_init(&fs_info->extent_ins);
cache_tree_init(&fs_info->fs_root_cache);
cache_tree_init(&fs_info->mapping_tree.cache_tree);
mutex_init(&fs_info->fs_mutex);
fs_info->fs_devices = fs_devices;
INIT_LIST_HEAD(&fs_info->dirty_cowonly_roots);
INIT_LIST_HEAD(&fs_info->space_info);
__setup_root(4096, 4096, 4096, 4096, tree_root,
fs_info, BTRFS_ROOT_TREE_OBJECTID);
if (writes)
ret = btrfs_open_devices(fs_devices, O_RDWR);
else
ret = btrfs_open_devices(fs_devices, O_RDONLY);
if (ret)
goto out_cleanup;
fs_info->super_bytenr = sb_bytenr;
disk_super = fs_info->super_copy;
ret = btrfs_read_dev_super(fs_devices->latest_bdev,
disk_super, sb_bytenr);
if (ret) {
printk("No valid btrfs found\n");
goto out_devices;
}
memcpy(fs_info->fsid, &disk_super->fsid, BTRFS_FSID_SIZE);
features = btrfs_super_incompat_flags(disk_super) &
~BTRFS_FEATURE_INCOMPAT_SUPP;
if (features) {
printk("couldn't open because of unsupported "
"option features (%Lx).\n",
(unsigned long long)features);
goto out_devices;
//.........这里部分代码省略.........
开发者ID:AK47POMA,项目名称:btrfs-progs,代码行数:101,代码来源:disk-io.c
示例4: main
int main(void)
{
posix_fadvise(1, 0, 0, POSIX_FADV_NORMAL);
return 0;
}
开发者ID:io7m,项目名称:coreland-sysdeps,代码行数:5,代码来源:posix_fadvise.c
示例5: setIoPrio
void eFilePushThread::thread()
{
setIoPrio(prio_class, prio);
off_t dest_pos = 0, source_pos = 0;
size_t bytes_read = 0;
off_t current_span_offset = 0;
size_t current_span_remaining = 0;
size_t written_since_last_sync = 0;
eDebug("FILEPUSH THREAD START");
/* we set the signal to not restart syscalls, so we can detect our signal. */
struct sigaction act;
act.sa_handler = signal_handler; // no, SIG_IGN doesn't do it. we want to receive the -EINTR
act.sa_flags = 0;
sigaction(SIGUSR1, &act, 0);
hasStarted();
source_pos = m_raw_source.lseek(0, SEEK_CUR);
/* m_stop must be evaluated after each syscall. */
while (!m_stop)
{
/* first try flushing the bufptr */
if (m_buf_start != m_buf_end)
{
/* filterRecordData wants to work on multiples of blocksize.
if it returns a negative result, it means that this many bytes should be skipped
*in front* of the buffer. Then, it will be called again. with the newer, shorter buffer.
if filterRecordData wants to skip more data then currently available, it must do that internally.
Skipped bytes will also not be output.
if it returns a positive result, that means that only these many bytes should be used
in the buffer.
In either case, current_span_remaining is given as a reference and can be modified. (Of course it
doesn't make sense to decrement it to a non-zero value unless you return 0 because that would just
skip some data). This is probably a very special application for fast-forward, where the current
span is to be cancelled after a complete iframe has been output.
we always call filterRecordData with our full buffer (otherwise we couldn't easily strip from the end)
we filter data only once, of course, but it might not get immediately written.
that's what m_filter_end is for - it points to the start of the unfiltered data.
*/
int filter_res;
do
{
filter_res = filterRecordData(m_buffer + m_filter_end, m_buf_end - m_filter_end, current_span_remaining);
if (filter_res < 0)
{
eDebug("[eFilePushThread] filterRecordData re-syncs and skips %d bytes", -filter_res);
m_buf_start = m_filter_end + -filter_res; /* this will also drop unwritten data */
ASSERT(m_buf_start <= m_buf_end); /* otherwise filterRecordData skipped more data than available. */
continue; /* try again */
}
/* adjust end of buffer to strip dropped tail bytes */
m_buf_end = m_filter_end + filter_res;
/* mark data as filtered. */
m_filter_end = m_buf_end;
} while (0);
ASSERT(m_filter_end == m_buf_end);
if (m_buf_start == m_buf_end)
continue;
/* now write out data. it will be 'aligned' (according to filterRecordData).
absolutely forbidden is to return EINTR and consume a non-aligned number of bytes.
*/
int w = write(m_fd_dest, m_buffer + m_buf_start, m_buf_end - m_buf_start);
// fwrite(m_buffer + m_buf_start, 1, m_buf_end - m_buf_start, f);
// eDebug("wrote %d bytes", w);
if (w <= 0)
{
if (errno == EINTR || errno == EAGAIN || errno == EBUSY)
continue;
eDebug("eFilePushThread WRITE ERROR");
sendEvent(evtWriteError);
break;
// ... we would stop the thread
}
written_since_last_sync += w;
if (written_since_last_sync >= 512*1024)
{
int toflush = written_since_last_sync > 2*1024*1024 ?
2*1024*1024 : written_since_last_sync &~ 4095; // write max 2MB at once
dest_pos = lseek(m_fd_dest, 0, SEEK_CUR);
dest_pos -= toflush;
posix_fadvise(m_fd_dest, dest_pos, toflush, POSIX_FADV_DONTNEED);
//.........这里部分代码省略.........
开发者ID:FFTEAM,项目名称:enigma2-5,代码行数:101,代码来源:filepush.cpp
示例6: verifyTorrent
static tr_bool
verifyTorrent( tr_torrent * tor, tr_bool * stopFlag )
{
time_t end;
SHA_CTX sha;
int fd = -1;
int64_t filePos = 0;
tr_bool changed = 0;
tr_bool hadPiece = 0;
time_t lastSleptAt = 0;
uint32_t piecePos = 0;
tr_file_index_t fileIndex = 0;
tr_file_index_t prevFileIndex = !fileIndex;
tr_piece_index_t pieceIndex = 0;
const time_t begin = tr_time( );
const size_t buflen = 1024 * 128; /* 128 KiB buffer */
uint8_t * buffer = tr_valloc( buflen );
SHA1_Init( &sha );
tr_tordbg( tor, "%s", "verifying torrent..." );
tr_torrentSetChecked( tor, 0 );
while( !*stopFlag && ( pieceIndex < tor->info.pieceCount ) )
{
uint32_t leftInPiece;
uint32_t bytesThisPass;
uint64_t leftInFile;
const tr_file * file = &tor->info.files[fileIndex];
/* if we're starting a new piece... */
if( piecePos == 0 )
hadPiece = tr_cpPieceIsComplete( &tor->completion, pieceIndex );
/* if we're starting a new file... */
if( !filePos && (fd<0) && (fileIndex!=prevFileIndex) )
{
char * filename = tr_torrentFindFile( tor, fileIndex );
fd = filename == NULL ? -1 : tr_open_file_for_scanning( filename );
tr_free( filename );
prevFileIndex = fileIndex;
}
/* figure out how much we can read this pass */
leftInPiece = tr_torPieceCountBytes( tor, pieceIndex ) - piecePos;
leftInFile = file->length - filePos;
bytesThisPass = MIN( leftInFile, leftInPiece );
bytesThisPass = MIN( bytesThisPass, buflen );
/* read a bit */
if( fd >= 0 ) {
const ssize_t numRead = tr_pread( fd, buffer, bytesThisPass, filePos );
if( numRead > 0 ) {
bytesThisPass = (uint32_t)numRead;
SHA1_Update( &sha, buffer, bytesThisPass );
#if defined HAVE_POSIX_FADVISE && defined POSIX_FADV_DONTNEED
posix_fadvise( fd, filePos, bytesThisPass, POSIX_FADV_DONTNEED );
#endif
}
}
/* move our offsets */
leftInPiece -= bytesThisPass;
leftInFile -= bytesThisPass;
piecePos += bytesThisPass;
filePos += bytesThisPass;
/* if we're finishing a piece... */
if( leftInPiece == 0 )
{
time_t now;
tr_bool hasPiece;
uint8_t hash[SHA_DIGEST_LENGTH];
SHA1_Final( hash, &sha );
hasPiece = !memcmp( hash, tor->info.pieces[pieceIndex].hash, SHA_DIGEST_LENGTH );
if( hasPiece || hadPiece ) {
tr_torrentSetHasPiece( tor, pieceIndex, hasPiece );
changed |= hasPiece != hadPiece;
}
tr_torrentSetPieceChecked( tor, pieceIndex );
now = tr_time( );
tor->anyDate = now;
/* sleeping even just a few msec per second goes a long
* way towards reducing IO load... */
if( lastSleptAt != now ) {
lastSleptAt = now;
tr_wait_msec( MSEC_TO_SLEEP_PER_SECOND_DURING_VERIFY );
}
SHA1_Init( &sha );
++pieceIndex;
piecePos = 0;
}
/* if we're finishing a file... */
if( leftInFile == 0 )
{
if( fd >= 0 ) { tr_close_file( fd ); fd = -1; }
//.........这里部分代码省略.........
开发者ID:dreamcat4,项目名称:transmission,代码行数:101,代码来源:verify.c
示例7: parse_makefile
/*
* parse (pseudo) makefile
*
* it may have only the following form:
*
* TARGETS = xxx ...
* INTERACTIVE = yyy ...
* aaa:
* bbb: xxx ddd ...
*
* other lines are ignored.
*/
void parse_makefile(const char *path)
{
FILE *fp;
char buf[LINE_MAX]; /* FIXME: is this enough big? */
char *s, *strp, *p;
struct makenode *node;
#if defined _XOPEN_SOURCE && (_XOPEN_SOURCE - 0) >= 600
int fd;
if (getuid() == (uid_t)0)
o_flags |= O_NOATIME;
if ((fd = open(path, o_flags)) < 0) {
fprintf(stderr, "Can't open %s: %s\n", path, strerror(errno));
exit(1);
}
(void)posix_fadvise(fd, 0, 0, POSIX_FADV_WILLNEED);
(void)posix_fadvise(fd, 0, 0, POSIX_FADV_SEQUENTIAL);
(void)posix_fadvise(fd, 0, 0, POSIX_FADV_NOREUSE);
if ((fp = fdopen(fd, "r")) == NULL)
#else
if ((fp = fopen(path, "r")) == NULL)
#endif
{
fprintf(stderr, "Can't open %s: %s\n", path, strerror(errno));
exit(1);
}
while (fgets(buf, sizeof(buf), fp)) {
for (s = buf; *s && isspace(*s); s++)
;
if (! *s || *s == '#')
continue;
if (! strncmp(s, "TARGETS =", 9)) {
s += 9;
strp = s;
while ((s = strsep(&strp, DELIMITER))) {
if (! *s)
continue;
add_target(s);
}
} else if (! strncmp(s, "INTERACTIVE =", 13)) {
s += 13;
strp = s;
while ((s = strsep(&strp, DELIMITER))) {
if (! *s)
continue;
mark_interactive(s);
}
} else {
p = strchr(s, ':');
if (! p)
continue;
*p = 0;
node = add_target(s);
strp = p + 1;
while ((s = strsep(&strp, DELIMITER))) {
if (! *s)
continue;
add_depend(node, s);
}
}
}
#if defined _XOPEN_SOURCE && (_XOPEN_SOURCE - 0) >= 600
(void)posix_fadvise(fd, 0, 0, POSIX_FADV_DONTNEED);
#endif
fclose(fp);
for (node = tree_list; node; node = node->next) {
int importance = 0;
if (! strcmp(node->name, "xdm")
|| ! strncmp(node->name, "gdm", 3)
|| ! strncmp(node->name, "kdm", 3)
|| ! strcmp(node->name, "boot.udev")
|| ! strcmp(node->name, "udev"))
importance = 100;
if (! strcmp(node->name, "sshd"))
importance = 2000;
if (! strncmp(node->name, "early", 5))
importance = 8000;
if (importance)
//.........这里部分代码省略.........
开发者ID:SaschaMester,项目名称:devuan-sysvinit,代码行数:101,代码来源:makeboot.c
示例8: measure
static void measure(int fd, struct flow *fw)
{
long delay;
fw->written_blocks++;
fw->total_written += fw->block_size;
if (fw->written_blocks < fw->blocks_per_delay)
return;
assert(!fdatasync(fd));
assert(!gettimeofday(&fw->t2, NULL));
/* Help the kernel to help us. */
assert(!posix_fadvise(fd, 0, 0, POSIX_FADV_DONTNEED));
delay = delay_ms(&fw->t1, &fw->t2);
switch (fw->state) {
case FW_INC:
if (delay > fw->delay_ms) {
move_to_search(fw,
fw->blocks_per_delay - fw->step / 2,
fw->blocks_per_delay);
} else if (delay < fw->delay_ms) {
inc_step(fw);
} else
move_to_steady(fw);
break;
case FW_DEC:
if (delay > fw->delay_ms) {
dec_step(fw);
} else if (delay < fw->delay_ms) {
move_to_search(fw, fw->blocks_per_delay,
fw->blocks_per_delay + fw->step / 2);
} else
move_to_steady(fw);
break;
case FW_SEARCH:
if (fw->bpd2 - fw->bpd1 <= 3) {
move_to_steady(fw);
break;
}
if (delay > fw->delay_ms) {
fw->bpd2 = fw->blocks_per_delay;
fw->blocks_per_delay = (fw->bpd1 + fw->bpd2) / 2;
} else if (delay < fw->delay_ms) {
fw->bpd1 = fw->blocks_per_delay;
fw->blocks_per_delay = (fw->bpd1 + fw->bpd2) / 2;
} else
move_to_steady(fw);
break;
case FW_STEADY:
update_mean(fw);
if (delay <= fw->delay_ms) {
move_to_inc(fw);
}
else if (fw->blocks_per_delay > 1) {
move_to_dec(fw);
}
break;
default:
assert(0);
}
if (fw->progress) {
/* Instantaneous speed. */
double inst_speed =
(double)fw->blocks_per_delay * fw->block_size * 1000 /
fw->delay_ms;
const char *unit = adjust_unit(&inst_speed);
double percent;
/* The following shouldn't be necessary, but sometimes
* the initial free space isn't exactly reported
* by the kernel; this issue has been seen on Macs.
*/
if (fw->total_size < fw->total_written)
fw->total_size = fw->total_written;
percent = (double)fw->total_written * 100 / fw->total_size;
erase(fw->erase);
fw->erase = printf("%.2f%% -- %.2f %s/s",
percent, inst_speed, unit);
assert(fw->erase > 0);
if (fw->measurements > 0)
fw->erase += pr_time(
(fw->total_size - fw->total_written) /
get_avg_speed(fw));
fflush(stdout);
}
start_measurement(fw);
}
开发者ID:benjaminma,项目名称:f3,代码行数:96,代码来源:f3write.c
示例9: pa_sound_file_load
int pa_sound_file_load(
pa_mempool *pool,
const char *fname,
pa_sample_spec *ss,
pa_channel_map *map,
pa_memchunk *chunk,
pa_proplist *p) {
SNDFILE *sf = NULL;
SF_INFO sfi;
int ret = -1;
size_t l;
sf_count_t (*readf_function)(SNDFILE *sndfile, void *ptr, sf_count_t frames) = NULL;
void *ptr = NULL;
int fd;
pa_assert(fname);
pa_assert(ss);
pa_assert(chunk);
pa_memchunk_reset(chunk);
if ((fd = pa_open_cloexec(fname, O_RDONLY, 0)) < 0) {
pa_log("Failed to open file %s: %s", fname, pa_cstrerror(errno));
goto finish;
}
#ifdef HAVE_POSIX_FADVISE
if (posix_fadvise(fd, 0, 0, POSIX_FADV_SEQUENTIAL) < 0) {
pa_log_warn("POSIX_FADV_SEQUENTIAL failed: %s", pa_cstrerror(errno));
goto finish;
} else
pa_log_debug("POSIX_FADV_SEQUENTIAL succeeded.");
#endif
pa_zero(sfi);
if (!(sf = sf_open_fd(fd, SFM_READ, &sfi, 1))) {
pa_log("Failed to open file %s", fname);
goto finish;
}
fd = -1;
if (pa_sndfile_read_sample_spec(sf, ss) < 0) {
pa_log("Failed to determine file sample format.");
goto finish;
}
if ((map && pa_sndfile_read_channel_map(sf, map) < 0)) {
if (ss->channels > 2)
pa_log("Failed to determine file channel map, synthesizing one.");
pa_channel_map_init_extend(map, ss->channels, PA_CHANNEL_MAP_DEFAULT);
}
if (p)
pa_sndfile_init_proplist(sf, p);
if ((l = pa_frame_size(ss) * (size_t) sfi.frames) > PA_SCACHE_ENTRY_SIZE_MAX) {
pa_log("File too large");
goto finish;
}
chunk->memblock = pa_memblock_new(pool, l);
chunk->index = 0;
chunk->length = l;
readf_function = pa_sndfile_readf_function(ss);
ptr = pa_memblock_acquire(chunk->memblock);
if ((readf_function && readf_function(sf, ptr, sfi.frames) != sfi.frames) ||
(!readf_function && sf_read_raw(sf, ptr, (sf_count_t) l) != (sf_count_t) l)) {
pa_log("Premature file end");
goto finish;
}
ret = 0;
finish:
if (sf)
sf_close(sf);
if (ptr)
pa_memblock_release(chunk->memblock);
if (ret != 0 && chunk->memblock)
pa_memblock_unref(chunk->memblock);
if (fd >= 0)
pa_close(fd);
return ret;
}
开发者ID:Distrotech,项目名称:pulseaudio,代码行数:94,代码来源:sound-file.c
示例10: FcDirCacheMapFd
/*
* Map a cache file into memory
*/
static FcCache *
FcDirCacheMapFd (FcConfig *config, int fd, struct stat *fd_stat, struct stat *dir_stat)
{
FcCache *cache;
FcBool allocated = FcFalse;
if (fd_stat->st_size < (int) sizeof (FcCache))
return NULL;
cache = FcCacheFindByStat (fd_stat);
if (cache)
{
if (FcCacheTimeValid (config, cache, dir_stat) &&
FcCacheDirsValid (config, cache))
return cache;
FcDirCacheUnload (cache);
cache = NULL;
}
/*
* Large cache files are mmap'ed, smaller cache files are read. This
* balances the system cost of mmap against per-process memory usage.
*/
if (FcCacheIsMmapSafe (fd) && fd_stat->st_size >= FC_CACHE_MIN_MMAP)
{
#if defined(HAVE_MMAP) || defined(__CYGWIN__)
cache = mmap (0, fd_stat->st_size, PROT_READ, MAP_SHARED, fd, 0);
#if (HAVE_POSIX_FADVISE) && defined(POSIX_FADV_WILLNEED)
posix_fadvise (fd, 0, fd_stat->st_size, POSIX_FADV_WILLNEED);
#endif
if (cache == MAP_FAILED)
cache = NULL;
#elif defined(_WIN32)
{
HANDLE hFileMap;
cache = NULL;
hFileMap = CreateFileMapping((HANDLE) _get_osfhandle(fd), NULL,
PAGE_READONLY, 0, 0, NULL);
if (hFileMap != NULL)
{
cache = MapViewOfFile (hFileMap, FILE_MAP_READ, 0, 0,
fd_stat->st_size);
CloseHandle (hFileMap);
}
}
#endif
}
if (!cache)
{
cache = malloc (fd_stat->st_size);
if (!cache)
return NULL;
if (read (fd, cache, fd_stat->st_size) != fd_stat->st_size)
{
free (cache);
return NULL;
}
allocated = FcTrue;
}
if (cache->magic != FC_CACHE_MAGIC_MMAP ||
cache->version < FC_CACHE_VERSION_NUMBER ||
cache->size != (intptr_t) fd_stat->st_size ||
!FcCacheTimeValid (config, cache, dir_stat) ||
!FcCacheDirsValid (config, cache) ||
!FcCacheInsert (cache, fd_stat))
{
if (allocated)
free (cache);
else
{
#if defined(HAVE_MMAP) || defined(__CYGWIN__)
munmap (cache, fd_stat->st_size);
#elif defined(_WIN32)
UnmapViewOfFile (cache);
#endif
}
return NULL;
}
/* Mark allocated caches so they're freed rather than unmapped */
if (allocated)
cache->magic = FC_CACHE_MAGIC_ALLOC;
return cache;
}
开发者ID:ConfusedReality,项目名称:pkg_fonts_fontconfig,代码行数:89,代码来源:fccache.c
示例11: __wt_block_write_off
//.........这里部分代码省略.........
* bytes. The assumption is applications with good compression support
* turn off checksums and assume corrupted blocks won't decompress
* correctly. However, if compression failed to shrink the block, the
* block wasn't compressed, in which case our caller will tell us to
* checksum the data to detect corruption. If compression succeeded,
* we still need to checksum the first WT_BLOCK_COMPRESS_SKIP bytes
* because they're not compressed, both to give salvage a quick test
* of whether a block is useful and to give us a test so we don't lose
* the first WT_BLOCK_COMPRESS_SKIP bytes without noticing.
*/
blk->flags = 0;
if (data_cksum)
F_SET(blk, WT_BLOCK_DATA_CKSUM);
blk->cksum = 0;
blk->cksum = __wt_cksum(
buf->mem, data_cksum ? align_size : WT_BLOCK_COMPRESS_SKIP);
if (!locked)
__wt_spin_lock(session, &block->live_lock);
ret = __wt_block_alloc(session, block, &offset, (off_t)align_size);
if (!locked)
__wt_spin_unlock(session, &block->live_lock);
WT_RET(ret);
#if defined(HAVE_POSIX_FALLOCATE) || defined(HAVE_FTRUNCATE)
/*
* Extend the file in chunks. We aren't holding a lock and we'd prefer
* to limit the number of threads extending the file at the same time,
* so choose the one thread that's crossing the extended boundary. We
* don't extend newly created files, and it's theoretically possible we
* might wait so long our extension of the file is passed by another
* thread writing single blocks, that's why there's a check in case the
* extended file size becomes too small: if the file size catches up,
* every thread will try to extend it.
*/
if (fh->extend_len != 0 &&
(fh->extend_size <= fh->size ||
(offset + fh->extend_len <= fh->extend_size &&
offset + fh->extend_len + align_size >= fh->extend_size))) {
fh->extend_size = offset + fh->extend_len * 2;
#if defined(HAVE_POSIX_FALLOCATE)
if ((ret =
posix_fallocate(fh->fd, offset, fh->extend_len * 2)) != 0)
WT_RET_MSG(
session, ret, "%s: posix_fallocate", fh->name);
#elif defined(HAVE_FTRUNCATE)
if ((ret = ftruncate(fh->fd, fh->extend_size)) != 0)
WT_RET_MSG(session, ret, "%s: ftruncate", fh->name);
#endif
}
#endif
if ((ret =
__wt_write(session, fh, offset, align_size, buf->mem)) != 0) {
if (!locked)
__wt_spin_lock(session, &block->live_lock);
WT_TRET(
__wt_block_off_free(session, block, offset, align_size));
if (!locked)
__wt_spin_unlock(session, &block->live_lock);
WT_RET(ret);
}
#ifdef HAVE_SYNC_FILE_RANGE
/*
* Optionally schedule writes for dirty pages in the system buffer
* cache.
*/
if (block->os_cache_dirty_max != 0 &&
(block->os_cache_dirty += align_size) > block->os_cache_dirty_max) {
block->os_cache_dirty = 0;
if ((ret = sync_file_range(fh->fd,
(off64_t)0, (off64_t)0, SYNC_FILE_RANGE_WRITE)) != 0)
WT_RET_MSG(
session, ret, "%s: sync_file_range", block->name);
}
#endif
#ifdef HAVE_POSIX_FADVISE
/* Optionally discard blocks from the system buffer cache. */
if (block->os_cache_max != 0 &&
(block->os_cache += align_size) > block->os_cache_max) {
block->os_cache = 0;
if ((ret = posix_fadvise(fh->fd,
(off_t)0, (off_t)0, POSIX_FADV_DONTNEED)) != 0)
WT_RET_MSG(
session, ret, "%s: posix_fadvise", block->name);
}
#endif
WT_CSTAT_INCR(session, block_write);
WT_CSTAT_INCRV(session, block_byte_write, align_size);
WT_VERBOSE_RET(session, write,
"off %" PRIuMAX ", size %" PRIu32 ", cksum %" PRIu32,
(uintmax_t)offset, align_size, blk->cksum);
*offsetp = offset;
*sizep = align_size;
*cksump = blk->cksum;
return (ret);
}
开发者ID:ckoolkarni,项目名称:wiredtiger,代码行数:101,代码来源:block_write.c
示例12: help_build_index
static void
help_build_index(help_file *h, int restricted)
{
long bigpos, pos = 0;
bool in_topic;
int i, lineno, ntopics;
size_t n;
char *s, *topic;
char the_topic[TOPIC_NAME_LEN + 1];
char line[LINE_SIZE + 1];
FILE *rfp;
tlist *cur;
/* Quietly ignore null values for the file */
if (!h || !h->file)
return;
if ((rfp = fopen(h->file, FOPEN_READ)) == NULL) {
do_rawlog(LT_ERR, "Can't open %s for reading: %s", h->file,
strerror(errno));
return;
}
if (restricted)
do_rawlog(LT_WIZ, "Indexing file %s (admin topics)", h->file);
else
do_rawlog(LT_WIZ, "Indexing file %s", h->file);
topics = NULL;
num_topics = 0;
top_topics = 0;
bigpos = 0L;
lineno = 0;
ntopics = 0;
in_topic = 0;
#ifdef HAVE_POSIX_FADVISE
posix_fadvise(fileno(rfp), 0, 0, POSIX_FADV_SEQUENTIAL);
#endif
while (fgets(line, LINE_SIZE, rfp) != NULL) {
++lineno;
if (ntopics == 0) {
/* Looking for the first topic, but we'll ignore blank lines */
if (!line[0]) {
/* Someone's feeding us /dev/null? */
do_rawlog(LT_ERR, "Malformed help file %s doesn't start with &",
h->file);
fclose(rfp);
return;
}
if (isspace(line[0]))
continue;
if (line[0] != '&') {
do_rawlog(LT_ERR, "Malformed help file %s doesn't start with &",
h->file);
fclose(rfp);
return;
}
}
n = strlen(line);
if (line[n - 1] != '\n') {
do_rawlog(LT_ERR, "Line %d of %s: line too long", lineno, h->file);
}
if (line[0] == '&') {
++ntopics;
if (!in_topic) {
/* Finish up last entry */
if (ntopics > 1) {
write_topic(pos);
}
in_topic = true;
}
/* parse out the topic */
/* Get the beginning of the topic string */
for (topic = &line[1];
(*topic == ' ' || *topic == '\t') && *topic != '\0'; topic++) ;
/* Get the topic */
strcpy(the_topic, "");
for (i = -1, s = topic; *s != '\n' && *s != '\0'; s++) {
if (i >= TOPIC_NAME_LEN - 1)
break;
if (*s != ' ' || the_topic[i] != ' ')
the_topic[++i] = *s;
}
if ((restricted && the_topic[0] == '&')
|| (!restricted && the_topic[0] != '&')) {
the_topic[++i] = '\0';
cur = (tlist *) malloc(sizeof(tlist));
strcpy(cur->topic, the_topic);
cur->next = top;
top = cur;
}
} else {
if (in_topic) {
pos = bigpos;
}
in_topic = false;
}
bigpos = ftell(rfp);
//.........这里部分代码省略.........
开发者ID:kymoon,项目名称:pennmush,代码行数:101,代码来源:help.c
示例13: __posix_open_file
//.........这里部分代码省略.........
pfh->direct_io = false;
#endif
#ifdef O_NOATIME
/* Avoid updating metadata for read-only workloads. */
if (file_type == WT_FS_OPEN_FILE_TYPE_DATA)
f |= O_NOATIME;
#endif
if (file_type == WT_FS_OPEN_FILE_TYPE_LOG &&
FLD_ISSET(conn->txn_logsync, WT_LOG_DSYNC)) {
#ifdef O_DSYNC
f |= O_DSYNC;
#elif defined(O_SYNC)
f |= O_SYNC;
#else
WT_ERR_MSG(session, ENOTSUP,
"unsupported log sync mode configured");
#endif
}
/* Create/Open the file. */
WT_SYSCALL_RETRY(((pfh->fd = open(name, f, mode)) == -1 ? -1 : 0), ret);
if (ret != 0)
WT_ERR_MSG(session, ret,
pfh->direct_io ?
"%s: handle-open: open: failed with direct I/O configured, "
"some filesystem types do not support direct I/O" :
"%s: handle-open: open", name);
#ifdef __linux__
/*
* Durability: some filesystems require a directory sync to be confident
* the file will appear.
*/
if (LF_ISSET(WT_FS_OPEN_DURABLE))
WT_ERR(__posix_directory_sync(session, name));
#endif
WT_ERR(__posix_open_file_cloexec(session, pfh->fd, name));
#if defined(HAVE_POSIX_FADVISE)
/*
* Disable read-ahead on trees: it slows down random read workloads.
* Ignore fadvise when doing direct I/O, the kernel cache isn't
* interesting.
*/
if (!pfh->direct_io && file_type == WT_FS_OPEN_FILE_TYPE_DATA) {
WT_SYSCALL(
posix_fadvise(pfh->fd, 0, 0, POSIX_FADV_RANDOM), ret);
if (ret != 0)
WT_ERR_MSG(session, ret,
"%s: handle-open: posix_fadvise", name);
}
#endif
directory_open:
/* Initialize public information. */
file_handle = (WT_FILE_HANDLE *)pfh;
WT_ERR(__wt_strdup(session, name, &file_handle->name));
file_handle->close = __posix_file_close;
#if defined(HAVE_POSIX_FADVISE)
/*
* Ignore fadvise when doing direct I/O, the kernel cache isn't
* interesting.
*/
if (!pfh->direct_io)
file_handle->fh_advise = __posix_file_advise;
#endif
file_handle->fh_allocate = __wt_posix_file_fallocate;
file_handle->fh_lock = __posix_file_lock;
#ifdef WORDS_BIGENDIAN
/*
* The underlying objects are little-endian, mapping objects isn't
* currently supported on big-endian systems.
*/
#else
file_handle->fh_map = __wt_posix_map;
#ifdef HAVE_POSIX_MADVISE
file_handle->fh_map_discard = __wt_posix_map_discard;
file_handle->fh_map_preload = __wt_posix_map_preload;
#endif
file_handle->fh_unmap = __wt_posix_unmap;
#endif
file_handle->fh_read = __posix_file_read;
file_handle->fh_size = __posix_file_size;
file_handle->fh_sync = __posix_file_sync;
#ifdef HAVE_SYNC_FILE_RANGE
file_handle->fh_sync_nowait = __posix_file_sync_nowait;
#endif
file_handle->fh_truncate = __posix_file_truncate;
file_handle->fh_write = __posix_file_write;
*file_handlep = file_handle;
return (0);
err: WT_TRET(__posix_file_close((WT_FILE_HANDLE *)pfh, wt_session));
return (ret);
}
开发者ID:GYGit,项目名称:mongo,代码行数:101,代码来源:os_fs.c
示例14: scr_flush_file_to_containers
/* given a filename, its meta data, its list of segments, and list of destination containers,
* copy file to container files */
static int scr_flush_file_to_containers(
const char* file,
scr_meta* meta,
scr_hash* segments,
const char* dst_dir)
{
/* check that we got something for a source file */
if (file == NULL || strcmp(file, "") == 0) {
scr_err("Invalid source file @ %s:%d",
__FILE__, __LINE__
);
return SCR_FAILURE;
}
/* check that our other arguments are valid */
if (meta == NULL || segments == NULL) {
scr_err("Invalid metadata or segments @ %s:%d",
__FILE__, __LINE__
);
return SCR_FAILURE;
}
/* open the file for reading */
int fd_src = scr_open(file, O_RDONLY);
if (fd_src < 0) {
scr_err("Opening file to copy: scr_open(%s) errno=%d %s @ %s:%d",
file, errno, strerror(errno), __FILE__, __LINE__
);
return SCR_FAILURE;
}
#if !defined(__APPLE__)
/* TODO:
posix_fadvise(fd, 0, 0, POSIX_FADV_DONTNEED | POSIX_FADV_SEQUENTIAL)
that tells the kernel that you don't ever need the pages
from the file again, and it won't bother keeping them in the page cache.
*/
posix_fadvise(fd_src, 0, 0, POSIX_FADV_DONTNEED | POSIX_FADV_SEQUENTIAL);
#endif
/* get the buffer size we'll use to write to the file */
unsigned long buf_size = scr_file_buf_size;
/* allocate buffer to read in file chunks */
char* buf = (char*) SCR_MALLOC(buf_size);
/* initialize crc value */
uLong crc;
if (scr_crc_on_flush) {
crc = crc32(0L, Z_NULL, 0);
}
int rc = SCR_SUCCESS;
/* write out each segment */
scr_hash_sort_int(segments, SCR_HASH_SORT_ASCENDING);
scr_hash_elem* elem;
for (elem = scr_hash_elem_first(segments);
elem != NULL;
elem = scr_hash_elem_next(elem))
{
/* get the container info for this segment */
scr_hash* hash = scr_hash_elem_hash(elem);
/* get the offset into the container and the length of the segment (both in bytes) */
char* container_name;
unsigned long container_offset, segment_length;
if (scr_container_get_name_offset_length(hash,
&container_name, &container_offset, &segment_length) != SCR_SUCCESS)
{
scr_err("Failed to get segment offset and length @ %s:%d",
__FILE__, __LINE__
);
rc = SCR_FAILURE;
break;
}
/* build full name to destination file */
scr_path* dst_path = scr_path_from_str(dst_dir);
scr_path_append_str(dst_path, container_name);
scr_path_reduce(dst_path);
char* dst_file = scr_path_strdup(dst_path);
/* open container file for writing -- we don't truncate here because more than one
* process may be writing to the same file */
int fd_container = scr_open(dst_file, O_WRONLY);
if (fd_container < 0) {
scr_err("Opening file for writing: scr_open(%s) errno=%d %s @ %s:%d",
dst_file, errno, strerror(errno), __FILE__, __LINE__
);
rc = SCR_FAILURE;
break;
}
#if !defined(__APPLE__)
/* TODO:
posix_fadvise(fd, 0, 0, POSIX_FADV_DONTNEED | POSIX_FADV_SEQUENTIAL)
that tells the kernel that you don't ever need the pages
//.........这里部分代码省略.........
开发者ID:LLNL,项目名称:scr,代码行数:101,代码来源:scr_flush_sync.c
示例15: rotate
bool rotate() {
if ( ! _enabled ) {
cout << "LoggingManager not enabled" << endl;
return true;
}
if ( _file ) {
#ifdef POSIX_FADV_DONTNEED
posix_fadvise(fileno(_file), 0, 0, POSIX_FADV_DONTNEED);
#endif
// Rename the (open) existing log file to a timestamped name
stringstream ss;
ss << _path << "." << terseCurrentTime( false );
string s = ss.str();
if (0 != rename(_path.c_str(), s.c_str())) {
error() << "Failed to rename " << _path << " to " << s;
return false;
}
}
FILE* tmp = 0; // The new file using the original logpath name
#if _WIN32
// We rename an open log file (above, on next rotation) and the trick to getting Windows to do that is
// to open the file with FILE_SHARE_DELETE. So, we can't use the freopen() call that non-Windows
// versions use because it would open the file without the FILE_SHARE_DELETE flag we need.
//
HANDLE newFileHandle = CreateFileA(
_path.c_str(),
GENERIC_WRITE,
FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE,
NULL,
OPEN_ALWAYS,
FILE_ATTRIBUTE_NORMAL,
NULL
);
if ( INVALID_HANDLE_VALUE != newFileHandle ) {
int newFileDescriptor = _open_osfhandle( reinterpret_cast<intptr_t>(newFileHandle), _O_APPEND );
tmp = _fdopen( newFileDescriptor, _append ? "a" : "w" );
}
#else
tmp = freopen(_path.c_str(), _append ? "a" : "w", stdout);
#endif
if ( !tmp ) {
cerr << "can't open: " << _path.c_str() << " for log file" << endl;
return false;
}
// redirect stdout and stderr to log file
dup2( fileno( tmp ), 1 ); // stdout
dup2( fileno( tmp ), 2 ); // stderr
Logstream::setLogFile(tmp); // after this point no thread will be using old file
#if _WIN32
if ( _file )
fclose( _file ); // In Windows, we still have the old file open, close it now
#endif
#if 0 // enable to test redirection
cout << "written to cout" << endl;
cerr << "written to cerr" << endl;
log() << "written to log()" << endl;
#endif
_file = tmp; // Save new file for next rotation
return true;
}
开发者ID:AK-Dominator,项目名称:mongo,代码行数:70,代码来源:log.cpp
示例16: main
int main(int argc, char ** argv)
{
ssize_t bytes_read = 0, bytes_read_pread = 0;
int fd1 = 0, fd2 = 0;
char data[READSIZE];
char data_pread[READSIZE];
struct aiocb aio;
int i;
struct timespec time1, time2;
off_t offset;
off_t len1, len2;
struct timeval start, end, result, totaltime, tmp;
if ( argc != 3 ) {
printf( "Usage: %s file-1-path file-2-path", argv[0] );
exit(1);
}
fd1 = open(argv[1], O_RDONLY);
if (fd1 == -1) {
perror("open");
return EXIT_FAILURE;
}
fd2 = open(argv[2], O_RDONLY);
if (fd2 == -1) {
perror("open");
return EXIT_FAILURE;
}
/* ************************************************ */
memset(&aio, 0, sizeof(aio));
memset(&data, 0, sizeof(data));
memset(&data_pread, 0, sizeof(data_pread));
aio.aio_fildes = fd1;
aio.aio_buf = data;
aio.aio_nbytes = READSIZE;
len1 = lseek(fd1, 0, SEEK_END);
assert(len1 >= 0);
timerclear(&totaltime);
srand(5);
//clock_gettime(TIMING_METHOD, &time1);
gettimeofday(&start, NULL);
for ( i = 0 ; i < REPTIMES ; i++ ) {
offset = len1 * (rand() / (double)RAND_MAX);
|
请发表评论