/[LeafOK_CVS]/lbbs/src/test_file_loader.c
ViewVC logotype

Diff of /lbbs/src/test_file_loader.c

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

Revision 1.1 by sysadm, Fri May 16 12:22:35 2025 UTC Revision 1.11 by sysadm, Tue Nov 4 14:58:56 2025 UTC
# Line 1  Line 1 
1  /***************************************************************************  /* SPDX-License-Identifier: GPL-3.0-or-later */
2                                                  file_loader.c  -  description  /*
3                                                           -------------------   * test_file_loader
4          Copyright            : (C) 2004-2025 by Leaflet   *   - tester for shared memory based file loader
5          Email                : leaflet@leafok.com   *
6   ***************************************************************************/   * Copyright (C) 2004-2025  Leaflet <leaflet@leafok.com>
7     */
 /***************************************************************************  
  *                                                                         *  
  *   This program is free software; you can redistribute it and/or modify  *  
  *   it under the terms of the GNU General Public License as published by  *  
  *   the Free Software Foundation; either version 3 of the License, or     *  
  *   (at your option) any later version.                                   *  
  *                                                                         *  
  ***************************************************************************/  
8    
9  #include "file_loader.h"  #include "file_loader.h"
10  #include "log.h"  #include "log.h"
11    #include "trie_dict.h"
12    #include <errno.h>
13  #include <stdio.h>  #include <stdio.h>
14  #include <unistd.h>  #include <unistd.h>
15    #include <sys/shm.h>
16    
17    #define TRIE_DICT_SHM_FILE "~trie_dict_shm.dat"
18    
19  const char *files[] = {  const char *files[] = {
20          "../data/welcome.txt",          "../data/welcome.txt",
21          "../data/copyright.txt",          "../data/copyright.txt",
22          "../data/register.txt",          "../data/register.txt",
23          "../data/license.txt",          "../data/license.txt",
24          "../data/login_error.txt"};          "../data/login_error.txt",
25            "../data/read_help.txt"};
26    
27  int files_cnt = 5;  int files_cnt = 6;
28    
29  int main(int argc, char *argv[])  int main(int argc, char *argv[])
30  {  {
31          int ret;          int ret;
         int file_loader_pool_size = 2;  
32          int i;          int i;
33          const FILE_MMAP *p_file_mmap;          const void *p_shm;
34            size_t data_len;
35            long line_total;
36            const void *p_data;
37            const long *p_line_offsets;
38            FILE *fp;
39    
40          if (log_begin("../log/bbsd.log", "../log/error.log") < 0)          if (log_begin("../log/bbsd.log", "../log/error.log") < 0)
41          {          {
# Line 41  int main(int argc, char *argv[]) Line 43  int main(int argc, char *argv[])
43                  return -1;                  return -1;
44          }          }
45    
46          log_std_redirect(STDOUT_FILENO);          log_common_redir(STDOUT_FILENO);
47          log_err_redirect(STDERR_FILENO);          log_error_redir(STDERR_FILENO);
48    
49            if ((fp = fopen(TRIE_DICT_SHM_FILE, "w")) == NULL)
50            {
51                    log_error("fopen(%s) error\n", TRIE_DICT_SHM_FILE);
52                    return -1;
53            }
54            fclose(fp);
55    
56            if (trie_dict_init(TRIE_DICT_SHM_FILE, TRIE_NODE_PER_POOL) < 0)
57            {
58                    printf("trie_dict_init failed\n");
59                    return -1;
60            }
61    
62          ret = file_loader_init(file_loader_pool_size);          ret = file_loader_init();
63          if (ret < 0)          if (ret < 0)
64          {          {
65                  printf("file_loader_init() error (%d)\n", ret);                  printf("file_loader_init() error (%d)\n", ret);
66                  return ret;                  return ret;
67          }          }
68    
69          ret = file_loader_init(file_loader_pool_size);          ret = file_loader_init();
70          if (ret == 0)          if (ret == 0)
71          {          {
72                  printf("Rerun file_loader_init() error\n");                  printf("Rerun file_loader_init() error\n");
# Line 59  int main(int argc, char *argv[]) Line 74  int main(int argc, char *argv[])
74    
75          printf("Testing #1\n");          printf("Testing #1\n");
76    
77          for (i = 0; i < file_loader_pool_size; i++)          for (i = 0; i < files_cnt; i++)
78          {          {
79                  if (load_file_mmap(files[i]) < 0)                  if (load_file(files[i]) < 0)
80                  {                  {
81                          printf("Load file %s error\n", files[i]);                          printf("Load file %s error\n", files[i]);
82                  }                  }
83          }          }
84    
85          for (i = 0; i < file_loader_pool_size; i++)          for (i = 0; i < files_cnt; i++)
86          {          {
87                  if ((p_file_mmap = get_file_mmap(files[i])) == NULL)                  if ((p_shm = get_file_shm_readonly(files[i], &data_len, &line_total, &p_data, &p_line_offsets)) == NULL)
88                  {                  {
89                          printf("Get file mmap %s error\n", files[i]);                          printf("Get file shm %s error\n", files[i]);
90                  }                  }
91                  else                  else
92                  {                  {
93                          printf("File: %s size: ", files[i]);                          printf("File: %s size: %ld lines: %ld\n", files[i], data_len, line_total);
                         printf("size: %ld lines: %ld\n", p_file_mmap->size, p_file_mmap->line_total);  
                 }  
         }  
94    
95          for (i = 0; i < file_loader_pool_size; i++)                          for (int j = 0; j < line_total; j++)
96          {                          {
97                  if (unload_file_mmap(files[i]) < 0)                                  printf("Line %d: %ld ~ %ld\n", j, p_line_offsets[j], p_line_offsets[j + 1]);
98                  {                          }
99                          printf("Unload file %s error\n", files[i]);  
100                            if (detach_file_shm(p_shm) < 0)
101                            {
102                                    log_error("detach_file_shm(%s) error\n", files[i]);
103                            }
104                  }                  }
105          }          }
106    
# Line 92  int main(int argc, char *argv[]) Line 108  int main(int argc, char *argv[])
108    
109          for (i = 0; i < files_cnt; i++)          for (i = 0; i < files_cnt; i++)
110          {          {
111                  if (i >= file_loader_pool_size)                  if (unload_file(files[i]) < 0)
112                  {                  {
113                          if (unload_file_mmap(files[i - file_loader_pool_size]) < 0)                          printf("Unload file %s error\n", files[i]);
                         {  
                                 printf("Unload file %s error\n", files[i]);  
                         }  
114                  }                  }
115            }
116    
117                  if (load_file_mmap(files[i]) < 0)          for (i = 0; i < files_cnt; i++)
118            {
119                    if (load_file(files[i]) < 0)
120                  {                  {
121                          printf("Load file %s error\n", files[i]);                          printf("Load file %s error\n", files[i]);
122                  }                  }
# Line 110  int main(int argc, char *argv[]) Line 126  int main(int argc, char *argv[])
126    
127          for (i = 0; i < files_cnt; i++)          for (i = 0; i < files_cnt; i++)
128          {          {
129                  if ((p_file_mmap = get_file_mmap(files[i])) == NULL)                  if (i % 2 == 0)
130                  {                  {
131                          printf("Get file mmap %s error\n", files[i]);                          if (unload_file(files[i]) < 0)
132                            {
133                                    printf("Unload file %s error\n", files[i]);
134                            }
135                  }                  }
136                  else          }
137    
138            for (i = 0; i < files_cnt; i++)
139            {
140                    if (i % 2 != 0)
141                  {                  {
142                          printf("File: %s size: ", files[i]);                          if ((p_shm = get_file_shm_readonly(files[i], &data_len, &line_total, &p_data, &p_line_offsets)) == NULL)
143                          printf("size: %ld lines: %ld\n", p_file_mmap->size, p_file_mmap->line_total);                          {
144                                    printf("Get file shm %s error\n", files[i]);
145                            }
146                            else
147                            {
148                                    printf("File: %s size: %ld lines: %ld\n", files[i], data_len, line_total);
149    
150                                    if (detach_file_shm(p_shm) < 0)
151                                    {
152                                            log_error("detach_file_shm(%s) error\n", files[i]);
153                                    }
154                            }
155                  }                  }
156          }          }
157    
158          file_loader_cleanup();          file_loader_cleanup();
159          file_loader_cleanup();          file_loader_cleanup();
160    
161            trie_dict_cleanup();
162    
163            if (unlink(TRIE_DICT_SHM_FILE) < 0)
164            {
165                    log_error("unlink(%s) error\n", TRIE_DICT_SHM_FILE);
166                    return -1;
167            }
168    
169          log_end();          log_end();
170    
171          return 0;          return 0;


Legend:
Removed lines/characters  
Changed lines/characters
  Added lines/characters

webmaster@leafok.com
ViewVC Help
Powered by ViewVC 1.3.0-beta1