MySQL 5.6.14 Source Code Document
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
my_file.c
1 /* Copyright (c) 2000, 2011, Oracle and/or its affiliates. All rights reserved.
2 
3  This program is free software; you can redistribute it and/or modify
4  it under the terms of the GNU General Public License as published by
5  the Free Software Foundation; version 2 of the License.
6 
7  This program is distributed in the hope that it will be useful,
8  but WITHOUT ANY WARRANTY; without even the implied warranty of
9  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10  GNU General Public License for more details.
11 
12  You should have received a copy of the GNU General Public License
13  along with this program; if not, write to the Free Software
14  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */
15 
16 #include "mysys_priv.h"
17 #include "my_static.h"
18 #include <m_string.h>
19 
20 /*
21  set how many open files we want to be able to handle
22 
23  SYNOPSIS
24  set_maximum_open_files()
25  max_file_limit Files to open
26 
27  NOTES
28  The request may not fulfilled becasue of system limitations
29 
30  RETURN
31  Files available to open.
32  May be more or less than max_file_limit!
33 */
34 
35 #if defined(HAVE_GETRLIMIT) && defined(RLIMIT_NOFILE)
36 
37 #ifndef RLIM_INFINITY
38 #define RLIM_INFINITY ((uint) 0xffffffff)
39 #endif
40 
41 static uint set_max_open_files(uint max_file_limit)
42 {
43  struct rlimit rlimit;
44  uint old_cur;
45  DBUG_ENTER("set_max_open_files");
46  DBUG_PRINT("enter",("files: %u", max_file_limit));
47 
48  if (!getrlimit(RLIMIT_NOFILE,&rlimit))
49  {
50  old_cur= (uint) rlimit.rlim_cur;
51  DBUG_PRINT("info", ("rlim_cur: %u rlim_max: %u",
52  (uint) rlimit.rlim_cur,
53  (uint) rlimit.rlim_max));
54  if (rlimit.rlim_cur == RLIM_INFINITY)
55  rlimit.rlim_cur = max_file_limit;
56  if (rlimit.rlim_cur >= max_file_limit)
57  DBUG_RETURN(rlimit.rlim_cur); /* purecov: inspected */
58  rlimit.rlim_cur= rlimit.rlim_max= max_file_limit;
59  if (setrlimit(RLIMIT_NOFILE, &rlimit))
60  max_file_limit= old_cur; /* Use original value */
61  else
62  {
63  rlimit.rlim_cur= 0; /* Safety if next call fails */
64  (void) getrlimit(RLIMIT_NOFILE,&rlimit);
65  DBUG_PRINT("info", ("rlim_cur: %u", (uint) rlimit.rlim_cur));
66  if (rlimit.rlim_cur) /* If call didn't fail */
67  max_file_limit= (uint) rlimit.rlim_cur;
68  }
69  }
70  DBUG_PRINT("exit",("max_file_limit: %u", max_file_limit));
71  DBUG_RETURN(max_file_limit);
72 }
73 
74 #else
75 static uint set_max_open_files(uint max_file_limit)
76 {
77  /* We don't know the limit. Return best guess */
78  return min(max_file_limit, OS_FILE_LIMIT);
79 }
80 #endif
81 
82 
83 /*
84  Change number of open files
85 
86  SYNOPSIS:
87  my_set_max_open_files()
88  files Number of requested files
89 
90  RETURN
91  number of files available for open
92 */
93 
94 uint my_set_max_open_files(uint files)
95 {
96  struct st_my_file_info *tmp;
97  DBUG_ENTER("my_set_max_open_files");
98  DBUG_PRINT("enter",("files: %u my_file_limit: %u", files, my_file_limit));
99 
100  files+= MY_FILE_MIN;
101  files= set_max_open_files(MY_MIN(files, OS_FILE_LIMIT));
102  if (files <= MY_NFILE)
103  DBUG_RETURN(files);
104 
105  if (!(tmp= (struct st_my_file_info*) my_malloc(sizeof(*tmp) * files,
106  MYF(MY_WME))))
107  DBUG_RETURN(MY_NFILE);
108 
109  /* Copy any initialized files */
110  memcpy((char*) tmp, (char*) my_file_info,
111  sizeof(*tmp) * MY_MIN(my_file_limit, files));
112  memset((tmp + my_file_limit), 0,
113  MY_MAX((int) (files - my_file_limit), 0) * sizeof(*tmp));
114  my_free_open_file_info(); /* Free if already allocated */
115  my_file_info= tmp;
116  my_file_limit= files;
117  DBUG_PRINT("exit",("files: %u", files));
118  DBUG_RETURN(files);
119 }
120 
121 
122 void my_free_open_file_info()
123 {
124  DBUG_ENTER("my_free_file_info");
125  if (my_file_info != my_file_info_default)
126  {
127  /* Copy data back for my_print_open_files */
128  memcpy((char*) my_file_info_default, my_file_info,
129  sizeof(*my_file_info_default)* MY_NFILE);
130  my_free(my_file_info);
131  my_file_info= my_file_info_default;
132  my_file_limit= MY_NFILE;
133  }
134  DBUG_VOID_RETURN;
135 }