~ps10gel/ubuntu/xenial/trafficserver/6.2.0

« back to all changes in this revision

Viewing changes to iocore/cache/I_Store.h

  • Committer: Bazaar Package Importer
  • Author(s): Arno Toell
  • Date: 2011-01-13 11:49:18 UTC
  • Revision ID: james.westby@ubuntu.com-20110113114918-vu422h8dknrgkj15
Tags: upstream-2.1.5-unstable
ImportĀ upstreamĀ versionĀ 2.1.5-unstable

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/** @file
 
2
 
 
3
  A brief file description
 
4
 
 
5
  @section license License
 
6
 
 
7
  Licensed to the Apache Software Foundation (ASF) under one
 
8
  or more contributor license agreements.  See the NOTICE file
 
9
  distributed with this work for additional information
 
10
  regarding copyright ownership.  The ASF licenses this file
 
11
  to you under the Apache License, Version 2.0 (the
 
12
  "License"); you may not use this file except in compliance
 
13
  with the License.  You may obtain a copy of the License at
 
14
 
 
15
      http://www.apache.org/licenses/LICENSE-2.0
 
16
 
 
17
  Unless required by applicable law or agreed to in writing, software
 
18
  distributed under the License is distributed on an "AS IS" BASIS,
 
19
  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 
20
  See the License for the specific language governing permissions and
 
21
  limitations under the License.
 
22
 */
 
23
 
 
24
/****************************************************************************
 
25
 
 
26
  Store.h
 
27
 
 
28
 
 
29
 ****************************************************************************/
 
30
 
 
31
#ifndef _Store_h_
 
32
#define _Store_h_
 
33
 
 
34
#include "libts.h"
 
35
 
 
36
#define STORE_BLOCK_SIZE       8192
 
37
#define STORE_BLOCK_SHIFT      13
 
38
#define DEFAULT_HW_SECTOR_SIZE 512
 
39
 
 
40
//
 
41
// A Store is a place to store data.
 
42
// Those on the same disk should be in a linked list.
 
43
//
 
44
struct Span
 
45
{
 
46
  char *pathname;
 
47
  int64_t blocks;
 
48
  int64_t hw_sector_size;
 
49
  bool file_pathname;           // the pathname is a file
 
50
  bool isRaw;
 
51
  int64_t offset;                 // used only if (file == true)
 
52
  int alignment;
 
53
  int disk_id;
 
54
  LINK(Span, link);
 
55
 
 
56
private:
 
57
    bool is_mmapable_internal;
 
58
public:
 
59
  bool is_mmapable() { return is_mmapable_internal; }
 
60
  void set_mmapable(bool s) { is_mmapable_internal = s; }
 
61
  int64_t size() { return blocks * STORE_BLOCK_SIZE; }
 
62
 
 
63
  int64_t total_blocks() {
 
64
    if (link.next) {
 
65
      return blocks + link.next->total_blocks();
 
66
    } else {
 
67
      return blocks;
 
68
    }
 
69
  }
 
70
 
 
71
  Span *nth(int i) {
 
72
    Span *x = this;
 
73
    while (x && i--)
 
74
      x = x->link.next;
 
75
    return x;
 
76
  }
 
77
 
 
78
  int paths() {
 
79
    int i = 0;
 
80
    for (Span * x = this; x; i++, x = x->link.next);
 
81
    return i;
 
82
  }
 
83
  int write(int fd);
 
84
  int read(int fd);
 
85
 
 
86
  Span *dup();
 
87
  int64_t end() { return offset + blocks; }
 
88
 
 
89
  const char *init(char *n, int64_t size);
 
90
 
 
91
  // 0 on success -1 on failure
 
92
  int path(char *filename,      // for non-file, the filename in the director
 
93
           int64_t * offset,      // for file, start offset (unsupported)
 
94
           char *buf, int buflen);      // where to store the path
 
95
 
 
96
Span():pathname(NULL), blocks(0), hw_sector_size(DEFAULT_HW_SECTOR_SIZE), file_pathname(false),
 
97
       isRaw(true), offset(0), alignment(0), disk_id(0), is_mmapable_internal(false) {
 
98
  }
 
99
  ~Span();
 
100
};
 
101
 
 
102
struct Store
 
103
{
 
104
  //
 
105
  // Public Interface
 
106
  // Thread-safe operations
 
107
  //
 
108
 
 
109
  // spread evenly on all disks
 
110
  void spread_alloc(Store & s, unsigned int blocks, bool mmapable = true);
 
111
  void alloc(Store & s, unsigned int blocks, bool only_one = false, bool mmapable = true);
 
112
 
 
113
  Span *alloc_one(unsigned int blocks, bool mmapable) {
 
114
    Store s;
 
115
      alloc(s, blocks, true, mmapable);
 
116
    if (s.n_disks)
 
117
    {
 
118
      Span *t = s.disk[0];
 
119
        s.disk[0] = NULL;
 
120
        return t;
 
121
    } else
 
122
        return NULL;
 
123
  }
 
124
  // try to allocate, return (s == gotten, diff == not gotten)
 
125
  void try_realloc(Store & s, Store & diff);
 
126
 
 
127
  // free back the contents of a store.
 
128
  // must have been JUST allocated (no intervening allocs/frees)
 
129
  void free(Store & s);
 
130
  void add(Span * s);
 
131
  void add(Store & s);
 
132
  void dup(Store & s);
 
133
  void sort();
 
134
  void extend(int i)
 
135
  {
 
136
    if (i > n_disks) {
 
137
      disk = (Span **) xrealloc(disk, i * sizeof(Span *));
 
138
      for (int j = n_disks; j < i; j++)
 
139
        disk[j] = NULL;
 
140
      n_disks = i;
 
141
    }
 
142
  }
 
143
 
 
144
  // Non Thread-safe operations
 
145
  unsigned int total_blocks(int after = 0) {
 
146
    int64_t t = 0;
 
147
    for (int i = after; i < n_disks; i++)
 
148
      if (disk[i])
 
149
        t += disk[i]->total_blocks();
 
150
    return (unsigned int) t;
 
151
  }
 
152
  // 0 on success -1 on failure
 
153
  // these operations are NOT thread-safe
 
154
  //
 
155
  int write(int fd, char *name);
 
156
  int read(int fd, char *name);
 
157
  int clear(char *filename, bool clear_dirs = true);
 
158
  void normalize();
 
159
  void delete_all();
 
160
  int remove(char *pathname);
 
161
  Store();
 
162
  ~Store();
 
163
 
 
164
  int n_disks;
 
165
  Span **disk;
 
166
 
 
167
  //
 
168
  // returns NULL on success
 
169
  // if fd >= 0 then on failure it returns an error string
 
170
  //            otherwise on failure it returns (char *)-1
 
171
  //
 
172
  const char *read_config(int fd = -1);
 
173
  int write_config_data(int fd);
 
174
};
 
175
 
 
176
extern Store theStore;
 
177
 
 
178
// store either free or in the cache, can be stolen for reconfiguration
 
179
void stealStore(Store & s, int blocks);
 
180
int initialize_store();
 
181
 
 
182
struct storageConfigFile {
 
183
  const char *parseFile(int fd) {
 
184
    Store tStore;
 
185
    return tStore.read_config(fd);
 
186
  }
 
187
};
 
188
 
 
189
#endif