- r - 以节省内存的方式增长 data.frame
- ruby-on-rails - ruby/ruby on rails 内存泄漏检测
- android - 无法解析导入android.support.v7.app
- UNIX 域套接字与共享内存(映射文件)
我正在寻找一种在 Linux 中生成短读的方法,这样我就可以对它们周围的处理代码进行单元测试。
我有许多方法可以在较低级别调用 pread/pread64 来读取文件系统中的文件。这些旨在处理发生短读取的情况(读取的字节数小于请求的字节数)。
我见过短读取发生的情况(跨网络文件系统)。
理想情况下,我将能够创建一个允许读取 N 个字节的文件,然后会发生 M 个字节的短读取,然后按预期进行正常读取。这将允许单元测试指向文件/文件系统。
谢谢!
最佳答案
如果您知道要拦截的库调用,您可以使用通过 LD_PRELOAD
加载的共享对象插入调用。
shortread.c:
#include <sys/types.h>
#include <dlfcn.h>
#define MAX_FDS 1024
static int short_read_array[ MAX_FDS ];
// #define these to match your system's values
// (need to be really careful with header files since
// getting open() declared would make things very
// difficult - just try this with open( const char *, int, ...);
// declared to see what I mean...)
#define O_RDONLY 0
#define O_WRONLY 1
#define O_RDWR 2
// note that the mode bits for read/write are
// not a bitwise-or - they are distinct values
#define MODE_BITS 3
// it's much easier to *NOT* even deal with the
// fact that open() is a varargs function
// but that means probably having to do some
// typedef's and #defines to get this to compile
// typedef some function points to make things easier
typedef int ( *open_ptr_t )( const char *name, int flags, mode_t mode );
typedef ssize_t ( *read_ptr_t )( int fd, void *buf, size_t bytes );
typedef int ( *close_ptr_t )( int fd );
// function points to the real IO library calls
static open_ptr_t real_open = NULL;
static read_ptr_t real_read = NULL;
static close_ptr_t real_close = NULL;
// this will return non-zero if 'filename' is a file
// to cause short reads on
static int shortReadsOnFd( const char *filename )
{
// add logic here based on the file name to
// return non-zero if you want to do
// short reads on this file
//
// return( 1 );
return( 0 );
}
// interpose on open()
int open( const char *filename, int flags, mode_t mode )
{
static pthread_mutex_t open_mutex = PTHREAD_MUTEX_INITIALIZER;
int fd;
pthread_mutex_lock( &open_mutex );
if ( NULL == real_open )
{
real_open = dlsym( RTLD_NEXT, "open" );
}
pthread_mutex_unlock( &open_mutex );
fd = real_open( filename, flags, mode );
if ( ( -1 == fd ) || ( fd >= MAX_FDS ) )
{
return( fd );
}
int mode_bits = flags & MODE_BITS;
// if the file can be read from, check if this is a file
// to do short reads on
if ( ( O_RDONLY == mode_bits ) || ( O_RDWR == mode_bits ) )
{
short_read_array[ fd ] = shortReadsOnFd( filename );
}
return( fd );
}
ssize_t read( int fd, void *buffer, size_t bytes )
{
static pthread_mutex_t read_mutex = PTHREAD_MUTEX_INITIALIZER;
if ( ( fd < MAX_FDS ) && ( short_read_array[ fd ] ) )
{
// read less bytes than the caller asked for
bytes /= 2;
if ( 0 == bytes )
{
bytes = 1;
}
}
pthread_mutex_lock( &read_mutex );
if ( NULL == real_read )
{
real_read = dlsym( RTLD_NEXT, "read" );
}
pthread_mutex_unlock( &read_mutex );
return( real_read( fd, buffer, bytes ) );
}
int close( int fd )
{
static pthread_mutex_t close_mutex = PTHREAD_MUTEX_INITIALIZER;
pthread_mutex_lock( &close_mutex );
if ( NULL == real_close )
{
real_close = dlsym( RTLD_NEXT, "close" );
}
pthread_mutex_unlock( &close_lock );
if ( fd < MAX_FDS )
{
short_read_array[ fd ] = 0;
}
return( real_close( fd ) );
}
用类似的东西编译:
gcc -shared [-m32|-m64] shortread.c -o libshortread.so
然后:
export LD_PRELOAD=/path/to/libshortread.so
要特别小心这样的 LD_PRELOAD - 进程树中的所有进程都将被强制加载库。如果 32 位进程必须加载 64 位库,它将无法运行,强制尝试加载 32 位库的 64 位进程也是如此。您可以向上面的源添加一个 init 函数,删除 LD_PRELOAD
环境变量(或将其设置为无害的东西)以稍微控制它。
如果任何应用程序使用 open()
的 O_DIRECT
标志,您可能还需要小心。修改读取的字节数可能会破坏某些 Linux 文件系统和/或实现的直接 IO,因为可能只支持页面大小的 IO 操作。
并且这段代码只处理read()
。您可能还需要处理 creat()
。还有 pread()
、readat()
、aio_read()
和 lio_listio()
,(甚至可能其他一些我现在想不起来了)虽然这不太可能。并注意处理大文件的 32 位进程。我已经有一段时间没有处理这些问题了,但我记得这可能会变得很糟糕。
另一个警告是 fopen()
和 fread()
等调用可能不会调用 open()
和 read ()
库调用可以直接发出相关的系统调用。在这种情况下,您将无法轻松修改这些调用的行为。插入整个可读取数据的基于 STDIO 的调用系列,如 fgets()
可能是一件非常困难的事情,如果不破坏东西的话。
如果您知道您的应用程序是单线程的,您可以删除互斥量。
关于c - 寻找一种在 Linux 中强制进行简短阅读的方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39585409/
我正在尝试读取一个大型日志文件,该文件已使用不同的分隔符(遗留更改)进行了解析。 此代码有效 import os, subprocess, time, re import pandas as pd f
我试图理解在 Linux 下以 Turbo 模式(特别是 fpc -Mtp -vw)编译的 Free Pascal 中看到的有点神奇的行为。代码来自 Jack Crenshaw 的“让我们构建一个编译
我有一个具有以下结构的 txt 文件: NAME DATA1 DATA2 a 10 1,2,3 b 6 8,9 c 2
我试图理解在 Linux 下以 Turbo 模式(特别是 fpc -Mtp -vw)编译的 Free Pascal 中看到的有点神奇的行为。代码来自 Jack Crenshaw 的“让我们构建一个编译
public class Bug1 { private String s; public void Bug1(){ s = "hello"; } public Stri
我们有这样一种情况,我们的应用程序需要处理一系列文件,而不是同步执行此功能,我们希望采用多线程将工作负载分配给不同的线程。 每一项工作是: 1.以只读方式打开文件 2.处理文件中的数据 3.将处理后的
我正在尝试读取 .php 文件并替换十六进制字符。php文件格式如下: 问题是它弄乱了转义字符 (\") 到目前为止我的代码: while(i=48 && str[i+2]=97 && str[i+
我正在用 C# 开发一个程序,我需要一些帮助。我正在尝试创建一个数组或项目列表,显示在某个网站上。我想要做的是阅读 anchor 文本,它是 href。例如,这是 HTML:
我有一个偏好设置,它控制我的应用程序是否在用户单击按钮时播放声音(这种情况经常发生,想想计算器)。每次用户单击按钮时,都会调用以下方法: private void playButtonClickSou
我正在尝试在我的标签末尾创建一个阅读更多按钮。我希望它默认显示 3 行。我正在用 swift 而不是 objective c 编写代码。只有当用户点击标签的阅读更多部分时,标签才会展开。它的外观和工作
当您获得第三方库(c、c++)、开源(LGPL 说)但没有很好的文档时,了解它以便能够集成到您的应用程序中的最佳方法是什么? 该库通常有一些示例程序,我最终使用 gdb 浏览了代码。还有其他建议/最佳
同时从 2 个或更多不同线程对同一个文件描述符使用 pread 是否有问题? 最佳答案 pread 本身是线程安全的,因为它不在 list of unsafe functions 上.所以调用它是安全
当您使用命令 pd.read_csv 读取 csv 时,如何跳过连续包含特定值的行?如果在第 50、55 行,第一列的值为 100,那么我想在读取 csv 文件时跳过这些行。我如何将这些命令放入像 p
我迫切需要在 C# 中使用 T4 生成 HTML 输出。 我正在使用 Runtime-T4-Files 并选择“TextTemplatingFilePreprocessor”而不是“TextTempl
今年夏天我在实习期间一直在学习 ERP 应用程序。由于我是一名即将毕业的程序员,我希望有一个可靠的软件分支可以帮助我完成工作,直到我确定下一步该做什么(直到我对大局有一个很好的了解)。到现在为止,我刚
将包含列(例如“a”、“b”)的数据帧保存为 parquet,然后在稍后的时间点读取 parquet 不会提供相同的列顺序(可能是“b”、“a”fe)文件保存为。 不幸的是,我无法弄清楚订单是如何受到
我正在开发一个使用谷歌表格作为数据库的应用程序,但我不知道如何让 Swift 从谷歌表格中读取。我浏览了 API 网站和一些问题,但刚开始我需要一些帮助。到目前为止,我有; 私有(private)让范
我打算阅读swing concept,如果值得一读,请推荐一些学习 Material 最佳答案 自 AWT 崩溃以来,Java 的 GUI 工具包太多了。即使是 Swing 也被评论家严重低估,但他们
我已经使用 J 几个月了,我发现阅读不熟悉的代码(例如,不是我自己写的)是该语言最具挑战性的方面之一,尤其是在默认情况下。过了一会儿,我想出了这个策略: 1)将代码段复制到word文档中 2)从(1)
很难说出这里问的是什么。这个问题是含糊的、模糊的、不完整的、过于宽泛的或修辞性的,无法以目前的形式得到合理的回答。如需帮助澄清此问题以便重新打开它,visit the help center 。 已关
我是一名优秀的程序员,十分优秀!