Product of Array Except Self

问题: Given an array arr[] of n integers, construct a Product Array prod[] (of the same size) such that prod[i] is equal to the product of all the elements of arr[] except arr[i]. 解答: 遍历数组循环跳过当前元素,需要两层循环, 时间复杂度O(n^2),空间复杂度O(n) def arrayOfProducts(array): products = [1 for _ in range(len(array))] for i in range(len(array)): runningProduct = 1 for j in range(len(array)): if i != j: runningProduct = runningProduct * array[j] products[i] = runningProduct return products ...

2024-01-08 · 1 min · 185 words

io_uring echo-server

io_uring io_uring是Linux特有的异步I/O API。io_uring的名称来自用户空间和内核空间之间共享的环形缓冲区。在内核和用户空间之间进行通信使用环形缓冲区作为主要的通信模式。这种思想在业务系统中还是挺常见的: 比如用MQ、Kafka消息队列推送信息。一个接收队列, 一个发送队列,另外设计上也有Actor模型的影子, 应用和kernel, 分别是两个actor, 而actor的邮箱就是 SQ, CQ两个队列, 对于应用程序, 消息发送到SQ, 从CQ中获取结果,对于kernel是从SQ中获取信息, 将处理结果发送到CQ中。 传统echoserver 做一个简单echoserver。 #include <arpa/inet.h> #include <assert.h> #include <stdio.h> #include <stdlib.h> #include <string.h> #include <unistd.h> #define BUF_SIZE 512 int main(int argc, char const* argv[]) { int serverfd, clientfd; struct sockaddr_in server, client; int len; int port = 1234; char buffer[BUF_SIZE]; if (argc == 2) { port = atoi(argv[1]); } serverfd = socket(AF_INET, SOCK_STREAM, 0); if (serverfd < 0) { perror("create socket"); exit(1); } server.sin_family = AF_INET; server.sin_addr.s_addr = INADDR_ANY; server.sin_port = htons(port); len = sizeof(server); if (bind(serverfd, (struct sockaddr*)&server, len) < 0) { perror("bind socket"); exit(1); } if (listen(serverfd, 2048) < 0) { perror("listen socket"); exit(1); } while (1) { len = sizeof(client); if ((clientfd = accept(serverfd, (struct sockaddr*)&client, &len)) < 0) { perror("accept error"); exit(4); } memset(buffer, 0, sizeof(buffer)); int size = read(clientfd, buffer, sizeof(buffer)); if (size < 0) { perror("read error"); exit(5); } if (size == 0) { close(clientfd); continue; } if (write(clientfd, buffer, size) < 0) { perror("write error"); exit(6); } close(clientfd); } close(serverfd); return 0; } 使用python写一个客户端 ...

2024-01-07 · 8 min · 1623 words

C struct检查成员是否存在

如何判断C struct中是否存在某一成员,最开始想法是通过offsetof宏定义 #define offsetof(st, m) \ ((size_t)&(((st *)0)->m)) C语言的offsetof()宏是ANSI C标准库中的一个特性,位于stddef.h头文件中。它用于计算给定结构体或联合体类型中某个成员的偏移量(以字节为单位), 但是这种方式仅能检查存在的成员, 不存在编译会有下面类似报错 XXX.c:11:35: error: ‘struct MyStruct’ has no member named ‘member3’; did you mean ‘member1’? 生成成员名的map C/C++ 不支持反射,与例如 Java 或 C# 不同。这意味着在 C/C++ 中,你不能在运行时"检测"未知结构(或类)的成员,你必须在编译时知道它 - 通常是通过 #include 包含定义结构的头文件。实际上,你不能访问未定义结构的内容 - 只能传递指针。 from https://cplusplus.com/forum/beginner/283156/ 同样作者给出了一个解决方案,那就是把对象放到map中, 对于C语言的,可以使用脚本处理一下。 大概思路根据脚本解析结构体,生成包含成员的数组,通过数组判断 # generate_struct_member_array.py import re def filter_content(content): # Remove comments content = re.sub(r'\/\*[\s\S]*?\*\/|\/\/.*', '', content) # Remove #define statements content = re.sub(r'#define\s+\w+\s+\w+', '', content) return content def parse_header(header_content): struct_pattern = re.compile(r'struct\s+(\w+)\s*{([^}]*)};', re.DOTALL) structs = struct_pattern.findall(header_content) struct_member_arrays = [] for struct_name, struct_body in structs: members = re.findall(r'\b\w+\s+(\w+)\s*(?:\[\w*\])?;', struct_body) struct_member_arrays.append((struct_name, members)) return struct_member_arrays def generate_array_code(struct_member_arrays): code = "" for struct_name, members in struct_member_arrays: code += f"const char *{struct_name}_member_names[] = {{\n" for member in members: code += f'\t"{member}",\n ' code = code.rstrip(', ') code += "};\n" return code def write_to_file(file_path, content): with open(file_path, 'w') as output_file: output_file.write(content) def read_and_generate(input_file_path, output_file_path): # Read the input header file content with open(input_file_path, 'r') as header_file: header_content = header_file.read() # Filter content (remove comments and #define statements) filtered_content = filter_content(header_content) # Parse the header file struct_member_arrays = parse_header(filtered_content) # Generate code for arrays array_code = generate_array_code(struct_member_arrays) # Write generated code to a new file write_to_file(output_file_path, array_code) print(f"Generated code has been written to {output_file_path}") # Specify the path to the input header file and the output header file input_header_file_path = 'hello.h' output_header_file_path = 'generated_define.h' # Read the input header file, generate code, and write to the output header file read_and_generate(input_header_file_path, output_header_file_path) hello.h ...

2024-01-01 · 4 min · 753 words

SNI

SNI是TLS协议扩展, 在握手的开始标识其尝试连接的主机名, 当多个HTTPS服务部署在同一IP地址上,客户端就可以通过这个标识指定它将使用哪一个服务, 同时服务端也无需使用相同的证书,它在概念上相当于HTTP/1.1基于名称的虚拟主机。SNI扩展最早在2003年的RFC 3546中出现。 HTTP服务通过 Http header “Host”, 来选择指定服务, HTTPS服务就是通过这个SNI来区分。通过可以通过nginx来验证一下 #user nobody; worker_processes 1; #error_log logs/error.log; error_log logs/error.log debug; #error_log logs/error.log info; #pid logs/nginx.pid; events { worker_connections 1024; } http { access_log logs/access.log ; server { listen 8443 ssl ; server_name test; ssl_certificate certs/cert.crt; ssl_certificate_key certs/cert.key; location / { return 200 "https-test\r\n"; } } server { listen 8443 ssl ; server_name garlic; ssl_certificate certs/cert2.crt; ssl_certificate_key certs/cert.key; location / { return 200 "https-garlic\r\n"; } } server { listen 8443 ssl ; server_name 10.10.10.10; ssl_certificate certs/cert3.crt; ssl_certificate_key certs/cert.key; location / { root html; index index.html index.htm; } } server { listen 9443 ssl ; server_name snitest; ssl_certificate certs/cert4.crt; ssl_certificate_key certs/cert.key; location / { root html; index index.html index.htm; } } server { listen 8080 ; server_name http-test; location / { return 200 "http-test\r\n"; } } server { listen 8080 ; server_name http-garlic; location / { return 200 "http-garlic\r\n"; } } } ...

2023-12-11 · 4 min · 648 words

LeetCode-longest-peak

问题 Given an array arr[] with N elements, the task is to find out the longest sub-array which has the shape of a mountain. A mountain sub-array consists of elements that are initially in ascending order until a peak element is reached and beyond the peak element all other elements of the sub-array are in decreasing order. Input: arr = [1, 3, 1, 4, 5, 6, 7, 8, 9, 8, 7, 6, 5] Output: 11 Explanation: There are two sub-arrays that can be considered as mountain sub-arrays. The first one is from index 0 – 2 (3 elements) and next one is from index 2 – 12 (11 elements). As 11 > 2, our answer is 11. ...

2023-11-26 · 2 min · 299 words

systemctl 启动服务缓慢

服务启动缓慢 使用systemd配置服务启动慢 strace -s 1024 systemctl start AAA 有 -1 : EAGAIN (Resource temporarily unavailable) 报错及卡顿, 查看了服务配置依赖项 /etc/systemd/system/multi-user.target.wants 目录下找到服务配置文件, 查看对应配置文件 Unit配置项里的Requires值, 查找对应依赖服务发现有一个服务异常, 这个服务启动脚本中启动network服务, 由于配置网卡错误导致无法启动。 systemd systemd 是 Linux 操作系统的系统和服务管理器系统。 当作为启动时的第一个进程(作为 PID 1)运行时,它充当 启动和维护用户空间服务的 init 系统。为登录用户启动单独的实例来启动他们的服务。 garlic@garlic:~$ ls -l /sbin/init lrwxrwxrwx 1 root root 20 Mar 20 2023 /sbin/init -> /lib/systemd/systemd garlic@garlic:~$ uname -a Linux garlic 6.2.0-35-generic #35-Ubuntu SMP PREEMPT_DYNAMIC Tue Oct 3 13:14:56 UTC 2023 x86_64 x86_64 x86_64 GNU/Linux 创建一个服务 之前写的应用软件自己写daemon进程,然后通过数据库完成配置管理,然后通过一个工具实现服务器启动关闭。简单的应用也是够用的。 ...

2023-10-21 · 2 min · 286 words

linux sendfile

文件复制 使用c语言实现一下文件复制功能: 第一个版本:使用fgetc, fputc。 #include <sys/types.h> #include <sys/stat.h> #include <fcntl.h> #include <sys/sendfile.h> #include <stdio.h> #include <unistd.h> int main(int argc, char **argv) { const char *fromfile = argv[1]; const char *tofile = argv[2]; char c; FILE *fromfd = fopen(fromfile, "r"); FILE *tofd = fopen(tofile, "w"); while ((c = fgetc(fromfd)) != EOF) { fputc(c, tofd); } fclose(fromfd); fclose(tofd); } 使用fallocate -l 512M a1.txt, 生成一个512M的文件 garlic@garlic:~/sendfile$ sudo strace -c ./fget a1.txt b1.txt % time seconds usecs/call calls errors syscall ------ ----------- ----------- --------- --------- ---------------- 58.23 4.191305 31 131072 write 41.77 3.006373 22 131074 read 0.00 0.000051 12 4 close 0.00 0.000000 0 8 mmap 0.00 0.000000 0 3 mprotect 0.00 0.000000 0 1 munmap 0.00 0.000000 0 3 brk 0.00 0.000000 0 2 pread64 0.00 0.000000 0 1 1 access 0.00 0.000000 0 1 execve 0.00 0.000000 0 2 1 arch_prctl 0.00 0.000000 0 1 set_tid_address 0.00 0.000000 0 4 openat 0.00 0.000000 0 4 newfstatat 0.00 0.000000 0 1 set_robust_list 0.00 0.000000 0 1 prlimit64 0.00 0.000000 0 1 getrandom 0.00 0.000000 0 1 rseq ------ ----------- ----------- --------- --------- ---------------- 100.00 7.197729 27 262184 2 total 使用strace统计一下结果, 耗时7s, 主要系统调用是read和write, 分别进行了13万左右的调用。(第二次运行快一些,应该是这部分数据还没有换出内存) ...

2023-09-16 · 23 min · 4773 words

linux 生成指定大小文件

linux生成指定大小文件常用的几个命令: fallocate truncate dd head tail fallocate...

2023-08-19 · 1 min · 180 words

getopt_long

getopt_long可以用来解析命令行参数,也可以进行参数解析。例如 #include <stdio.h> /* for printf */ #include <stdlib.h> /* for exit */ #include <getopt.h> static int verbose_flag; int parse(int argc, char **argv) { int c; int digit_optind = 0; while (1) { int this_option_optind = optind ? optind : 1; int option_index = 0; static struct option long_options[] = { {"verbose", no_argument, &verbose_flag, 1}, {"add", required_argument, 0, 0 }, {"append", no_argument, 0, 0 }, {"delete", required_argument, 0, 0 }, {"verbose", no_argument, 0, 0 }, {"create", required_argument, 0, 'c'}, {"file", required_argument, 0, 0 }, {0, 0, 0, 0 } }; c = getopt_long(argc, argv, "abc:d:012", long_options, &option_index); if (c == -1) break; switch (c) { case 0: printf("option %s", long_options[option_index].name); if (optarg) printf(" with arg %s", optarg); printf("\n"); break; case '0': case '1': case '2': if (digit_optind != 0 && digit_optind != this_option_optind) printf("digits occur in two different argv-elements.\n"); digit_optind = this_option_optind; printf("option %c\n", c); break; case 'a': printf("option a\n"); break; case 'b': printf("option b\n"); break; case 'c': printf("option c with value '%s'\n", optarg); break; case 'd': printf("option d with value '%s'\n", optarg); break; case '?': break; default: printf("?? getopt returned character code 0%o ??\n", c); } } if (optind < argc) { printf("non-option ARGV-elements: "); while (optind < argc) printf("%s ", argv[optind++]); printf("\n"); } if (verbose_flag) printf ("verbose flag is set\n"); return 0; } void main() { char *argv[6]={0}; int argc = 6; argv[0] = "prog1"; argv[1] = "--add"; argv[2] = "AA"; argv[3] = "-d"; argv[4] = "BB"; argv[5] = "--verbose"; parse(argc, argv); optind = 1; char *argv2[4]={0}; int argc2 = 4; argv2[0] = "prog2"; argv2[1] = "--file"; argv2[2] = "CC"; argv2[3] = "--verbose"; //argv2[3] = "--delete"; //argv2[4] = "DD"; parse(argc2, argv2); return ; } ...

2023-08-09 · 3 min · 617 words

A kernel debugger in Python: drgn

原文链接:https://lwn.net/Articles/789641/ 一个内核调试器,允许 Python 脚本在运行中的内核中访问数据结构,是Omar Sandoval在2019年Linux存储、文件系统和内存管理峰会(LSFMM)上的主题演讲。在他在Facebook的日常工作中,Sandoval进行了大量的内核调试,但他发现现有的工具还不足够满足他的需求。这促使他开发了drgn,一个内嵌在Python库中的调试器。 Sandoval开始进行了drgn(发音为“dragon”)的快速演示。他登录到一个虚拟机(VM),并使用"drgn -k"命令调用了正在运行的内核的调试器。在python交互界面中,他使用一些简单的Python代码,能够检查根文件系统的超级块,并遍历该超级块中缓存的索引节点(inodes)以及它们的路径。然后,他做了一些“稍微复杂一点的事情”,仅列出了大小大于1MB的文件的索引节点。这显示了一些更大的内核模块、库文件、systemd等。 他主要从事Btrfs和块层的工作,但他也经常调试各种随机的内核问题。Facebook拥有如此多的机器,以至于总会出现“超级稀有、百万分之一”的bug。他经常自愿去查看这些问题。在这个过程中,他习惯了使用GDB、crash和eBPF等工具,但他发现他经常希望能够对内核数据结构进行任意复杂的分析,这就是为什么他最终开发了drgn。 他说GDB有一些很好的特性,包括能够漂亮地打印类型、变量和表达式。但它专注于断点式调试,而在生产系统上他无法这样调试。它有一个脚本接口,但它很笨重,只是对现有的GDB命令进行了包装。 Crash是专门用于内核调试的工具;它了解链表、结构体、进程等等。但是如果你想超越这些东西,你就会遇到困难,Sandoval说。它不是特别灵活;当他使用它时,他经常不得不转储大量的状态,然后进行后处理。 BPF和BCC非常强大,他经常使用它们,但它们局限于在能够实时重现bug的情况下使用。他所看到的很多bug是几个小时前发生的,并导致机器锁死,或者他获得了一个核心转储文件,想要弄清楚发生了什么。BPF并不能真正涵盖这种用例;它更适用于跟踪,而不是一个真正的交互式调试器。 Drgn使得我们可以用一种真正的编程语言来编写实际的程序——当然,这要取决于个人对Python的看法。它比将数据转储到文本文件中,然后使用shell脚本进行处理,或者使用Python绑定的GDB要好得多。他有时将drgn称为“作为库的调试器”,因为它不仅提供一个有限的命令提示符;相反,它神奇地包装了类型、变量等等,让你可以随心所欲地使用它们。上面链接的用户指南和主页是开始了解drgn的好地方,你可以在那里了解它的全部功能。 他进行了另一个演示,展示了drgn的一些强大之处。它有交互和脚本模式。他首先进入了一个交互式会话,查看了一些变量,并指出drgn返回一个表示该变量的对象;该对象还包含额外的信息,如类型(也是一个对象)、地址和当然还有值。但是人们也可以实现列表迭代,他通过从init任务(task_struct结构)开始,沿着其子任务链一直追踪下去来展示这一点。 在演示中,虽然他实时编写了列表迭代的代码,但他指出如果你每次都需要这样做,那会变得很繁琐。Drgn提供了许多辅助函数来完成这些操作。目前,大多数辅助函数是用于文件系统和块层的,但也可以为网络和其他子系统添加更多的辅助函数。 他重新演示了他和同事在一个生产服务器的虚拟机上进行的实际调查,该虚拟机中成功复现了bug。该生产负载是一个用于存储冷数据的存储服务器;在该服务器上,长时间未使用的磁盘会被关闭以节省电力。因此,该服务器的磁盘经常频繁开关,这暴露了内核的bug。冷存储服务在一个容器中运行,有报告称停止该容器有时会花费很长时间 他开始分析这个问题时,意识到容器最终会完成,但需要很长时间。这暗示了可能存在某种泄漏。他展示了从块控制组数据结构逐步深入的过程,并使用Python的Set对象类型来跟踪与块控制组关联的唯一请求队列的数量。他还能够深入研究与用于标识请求队列的ID分配器(IDA)相关联的基数树(radix tree),以检查一些结果。最后,确定请求队列泄漏是由于引用循环引起的。 他提到了另一个案例,他使用drgn调试了Btrfs意外返回ENOSPC的问题。结果发现这是因为为孤立文件预留了额外的元数据空间。一旦确定了原因,很容易找出是哪个应用程序在创建这些孤立文件;可以周期性地重新启动该应用程序,直到对Btrfs进行真正的修复。此外,当他在内核中遇到一个新的子系统时,他通常会使用drgn来弄清楚所有组件是如何连接在一起的。 drgn的核心是一个名为libdrgn的C库。他说,如果你不喜欢Python而喜欢错误处理,你可以直接使用它。它有可插拔的后端,用于读取各种类型的内存,包括用于运行中的内核的/proc/kcore,崩溃转储,或者用于运行中程序的/proc/PID/mem。它使用DWARF来获取类型和符号,这并不是最方便的格式。他花了很多时间优化对DWARF数据的访问。该接口也是可插拔的,但到目前为止,他只实现了DWARF接口。 这些优化工作使得drgn的启动时间大约为半秒,而crash的启动时间约为15秒。由于drgn启动速度快,使用频率会更高;他仍然不喜欢不得不启动crash的情况。 drgn内嵌了一个C解释器的子集。这使得drgn能够正确处理许多边界情况,比如隐式转换和整数提升。虽然有些棘手并需要花费一些精力,但这意味着他在内核中运行的代码在转换后的代码中没有出现任何问题。 他说,最大的缺失功能是回溯支持。目前,你只能访问全局变量,这并不是一个巨大的限制,但他有时不得不使用crash来获取地址和其他信息,然后将它们插入drgn。这是“在drgn中完全可能实现的功能”,但他还没有做到。他希望使用BPF Type Format (BTF)而不是DWARF,因为它更小更简单。但主要限制是BTF不能处理变量;如果BTF能够处理变量,他将使用它。还在筹备中的是一个有用的drgn脚本和工具的存储库。 他一直在纠结如何将drgn与BPF和BCC集成。想法是在某种程度上使用BPF进行实时调试,而用drgn进行事后调试。两者之间有一些重叠,但他还没有完全弄清楚如何统一它们。BPF由于缺乏循环而使用起来有些麻烦,但drgn无法在事件发生时捕捉问题。他有一个“疯狂的疯狂想法”,就是让BPF断点调用一个用户空间的drgn程序,但他不确定这是否可能实现。 图片from 陳丁光

2023-08-01 · 1 min · 28 words