在 Mac 和 CentOS 上使用 vmalloc (libvmalloc.a) - 无法包含 vmalloc 标头

发布于 2024-11-24 16:47:22 字数 2122 浏览 1 评论 0原文

我想分配几乎连续的范围内存,以便在访问数据时可以使用局部性属性(空间局部性),同时考虑到更好的性能。我在下一页发现我需要使用 vmalloc 来更好地访问内存位置(如果我错了并且必须使用 kmalloc ,请纠正我)。

vmalloc 和 kmalloc 之间有什么区别?

我下载了vmalloc 包来自 http://www2.research.att.com/~gsf/cgi-bin/download.cgi?action=list&name=vmalloc

我按照安装过程构建了 libmalloc.a 静态库从源文件中,然后我将生成的 libvmalloc.a 库复制到我的 mac 上的 /usr/local/lib 和 /usr/lib 目录。

在我的 C 程序中,我尝试通过以下各种方法包含 vmalloc.h 头文件

#include <vmalloc.h>

#include <linux/vmalloc.h>

#include "vmalloc.h"

但它们都不起作用。我总是收到 vmalloc.h: No such file or directory 错误消息。 我在编译 C++ 程序时使用了 -L/usr/local/lib -lvmalloc 标志。 在我的台式计算机上(在 CentOS 操作系统下)尝试相同的操作时,我也遇到了同样的错误。
这是我的 makefile:

SHELL = /bin/sh
PROJECT = hw2

TARGET = mkhashtbl
CFLAGS = -O3
LFLAGS = -O3 -L/usr/local/lib -lvmalloc

TFILE = $(PROJECT).tgz
ZFILE = $(PROJECT).zip
PACKFILES = Makefile hashtbl.h hashtbl.c mkhashtbl.c timer.c

all:    $(TARGET)

run:    all
    - ./$(TARGET)

我还尝试修改我的链接器标志,如下所示:

LFLAGS = -O3 -L/usr/local/lib -lvmalloc

但我仍然遇到相同的错误。 在这种情况下可能出了什么问题?我链接库的方式是否有问题,或者 vmalloc 只适用于某些版本的 Linux?如果是后一种情况,我确信我至少应该仍然能够包含头文件。

编辑

我真正的问题实际上如下:

hashtbl_cache *    hashTable_cache;             /* Hash table cache */

    int i;
    hashTable_cache = (hashtbl_cache*)malloc(tbl_size* sizeof(hashtbl_cache));

    for( i = 0 ; i < tbl_size; i++ )
    {
          hashTable_cache[i]. ht_elements = (hashelt_cache*)malloc( hashTable->data_total_size[i] * sizeof(hashelt_cache) ) ;
    }     

我想确保每个缓存中的所有 ht_elements 都是按顺序创建的。我从论坛上了解到,vmalloc 非常适合创建缓存感知应用程序,因为数据是在虚拟内存中创建的。是否有任何其他方法可以确保缓存数组的所有元素的分配都是按连续顺序创建的,从而使我能够进行快速查找?另一件事是,每个缓存中每个元素的大小都不相同,所以我想使用 calloc 不是一个解决方案,但我可能是错的。

I want to allocate virtually contiguous range memory so that I can use the locality property (spatial locality) when accessing data, with consideration for better performance. I found out at the following page that I need to use vmalloc for better memory locality access (please correct me if I am wrong and have to use kmalloc instead).

What is the difference between vmalloc and kmalloc?

I downloaded the vmalloc package from http://www2.research.att.com/~gsf/cgi-bin/download.cgi?action=list&name=vmalloc

I followed the installation procedure for building the libmalloc.a static library from the source files, and then I copied the generated libvmalloc.a library to /usr/local/lib and /usr/lib directory on my mac.

In my C program, I tried to include the vmalloc.h header file through various approaches as follow:

#include <vmalloc.h>

.

#include <linux/vmalloc.h>

.

#include "vmalloc.h"

but none of them worked. I always got the vmalloc.h: No such file or directory error message.
I used -L/usr/local/lib -lvmalloc flags when compiling my C++ program.
I also got the same error when trying the same thing on my desktop computer (under CentOS operating system).
Here is my makefile:

SHELL = /bin/sh
PROJECT = hw2

TARGET = mkhashtbl
CFLAGS = -O3
LFLAGS = -O3 -L/usr/local/lib -lvmalloc

TFILE = $(PROJECT).tgz
ZFILE = $(PROJECT).zip
PACKFILES = Makefile hashtbl.h hashtbl.c mkhashtbl.c timer.c

all:    $(TARGET)

run:    all
    - ./$(TARGET)

I also tried to modify my Linker flags as follows:

LFLAGS = -O3 -L/usr/local/lib -lvmalloc

and I still got the same error.
What could be wrong in this case? Is there anything wrong in the way I linked the library, or does vmalloc only work for some versions of Linux? If it is the latter case, I am sure that I should still be able to at least include the header file.

EDIT

My real problem is actually the following:

hashtbl_cache *    hashTable_cache;             /* Hash table cache */

    int i;
    hashTable_cache = (hashtbl_cache*)malloc(tbl_size* sizeof(hashtbl_cache));

    for( i = 0 ; i < tbl_size; i++ )
    {
          hashTable_cache[i]. ht_elements = (hashelt_cache*)malloc( hashTable->data_total_size[i] * sizeof(hashelt_cache) ) ;
    }     

I want to make sure that all the ht_elements in every cache are created in order. I read from a forum that vmalloc is good for creating cache aware application, as data are created in virtual memory. Is there any other approach to make sure that the allocation of all elements of my cache array are created in contiguous order, hence will enable me to do a fast lookup? One more thing, the size of every elements in every cache is not the same, so I guess using calloc is not a solution, but I may be wrong.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

淡淡的优雅 2024-12-01 16:47:22

尼莫的评论应该作为答案给出:

该问题中的 vmalloc 是一个 Linux 内核函数。除非您正在破解内核或编写设备驱动程序,否则它与您无关。 AT&T 研究站点的 vmalloc 完全是另一回事。两者都没有按照你的想法去做。只需使用malloc即可。

很明显,您不知道“虚拟连续范围”的含义,或者您会意识到 malloc 必须 为您提供这一点;否则它根本不起作用。

过早的优化是万恶之源。尤其是当您不知道您想要进行的优化意味着什么时。

Nemo's comment should have been given as an answer:

The vmalloc in that question is a Linux kernel function. Unless you are hacking the kernel or writing a device driver, it is not relevant to you. The vmalloc at the AT&T Research site is something else entirely. Neither one does what you think it does. Just use malloc.

It's obvious that you don't know what "virtually contiguous range" means or you'd realize that malloc must give you that; otherwise it would not work at all.

Premature optimization is the root of all evil. Especially when you don't have any idea what the optimization you're trying to make means.

嘿咻 2024-12-01 16:47:22

编译时,指定-Ipath让gcc知道'vmalloc.h'在哪里。

或者,在调用 gcc 之前指定环境变量 C_INCLUDE_PATH(对于 C)或 CPLUS_INCLUDE_PATH(对于 C++)。

GCC 将按照以下方式搜索头文件:

  1. -Ipath
  2. 环境变量:C_INCLUDE_PATH、CPLUS_INCLUDE_PATH、OBJC_INCLUDE_PATH
  3. 默认路径(如果安装 gcc 时没有指定前缀),可能类似于:

    /usr/include

    /usr/local/include

    /usr/lib/gcc-lib/i386-linux/2.95.2/include

    /usr/lib/gcc-lib/i386-linux/2.95.2/../../../../include/g++-3

    /usr/lib/gcc-lib/i386-linux/2.95.2/../../../../i386-linux/include

When compiling, specify -Ipath for gcc to know where 'vmalloc.h' is.

Or, specify the environment variable C_INCLUDE_PATH (for C) or CPLUS_INCLUDE_PATH (for C++) before calling gcc.

GCC will search header files follow this way:

  1. -Ipath
  2. Environment variables: C_INCLUDE_PATH, CPLUS_INCLUDE_PATH, OBJC_INCLUDE_PATH
  3. Default paths (if you didn't specify prefix when installing gcc), probably like:

    /usr/include

    /usr/local/include

    /usr/lib/gcc-lib/i386-linux/2.95.2/include

    /usr/lib/gcc-lib/i386-linux/2.95.2/../../../../include/g++-3

    /usr/lib/gcc-lib/i386-linux/2.95.2/../../../../i386-linux/include

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文