chiark / gitweb /
1.0.6 update: Memory mapping features and stability fixes
[stressapptest] / src / disk_blocks.h
1 // Copyright 2008 Google Inc. All Rights Reserved.
2
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6
7 //      http://www.apache.org/licenses/LICENSE-2.0
8
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14
15 // Interface for a thread-safe container of disk blocks
16
17 #ifndef STRESSAPPTEST_DISK_BLOCKS_H_
18 #define STRESSAPPTEST_DISK_BLOCKS_H_
19
20 #include <sys/types.h>
21 #include <pthread.h>
22 #include <time.h>
23 #include <sys/time.h>
24 #include <errno.h>
25 #include <map>
26 #include <vector>
27 #include <string>
28 // This file must work with autoconf on its public version,
29 // so these includes are correct.
30 #include "pattern.h"
31
32 // Data about a block written to disk so that it can be verified later.
33 class BlockData {
34  public:
35   BlockData();
36   ~BlockData();
37   void SetParameters(int64 address, int64 size);
38   void IncreaseReferenceCounter();
39   void DecreaseReferenceCounter();
40   int GetReferenceCounter();
41   void SetBlockAsInitialized();
42   bool BlockIsInitialized();
43   int64 GetAddress();
44   int64 GetSize();
45   void SetPattern(Pattern *p);
46   Pattern *GetPattern();
47  protected:
48   int64 addr_;         // address of first sector in block
49   int64 size_;         // size of block
50   int references_;      // reference counter
51   bool initialized_;     // flag indicating the block was written on disk
52   Pattern *pattern_;
53   pthread_mutex_t data_mutex_;
54   DISALLOW_COPY_AND_ASSIGN(BlockData);
55 };
56
57 // Disk Block table - store data from blocks to be write / read by
58 // a DiskThread
59 class DiskBlockTable {
60  public:
61   DiskBlockTable();
62   virtual ~DiskBlockTable();
63
64   // Get Number of elements stored on table
65   int64 NumElems();
66   // Clean all table data
67   void CleanTable();
68   // Get a random block from the list. Only returns if a element
69   // is available (consider that other thread must have added them.
70   BlockData *GetRandomBlock();
71   // Set all initial parameters. Assumes all existent data is
72   // invalid and, therefore, must be removed.
73   void SetParameters(int sector_size, int write_block_size,
74                      int64 device_sectors,
75                      int64 segment_size,
76                      string device_name);
77   // Return a new block in a unused address.
78   BlockData *GetUnusedBlock(int64 segment);
79   // Remove block from structure (called by write threads)
80   int RemoveBlock(BlockData *block);
81   // Release block to be erased (called by random threads)
82   int ReleaseBlock(BlockData *block);
83
84  protected:
85
86   void InsertOnStructure(BlockData *block);
87   //  Generate a random 64-bit integer (virtual so it could be
88   //  override by the tests)
89   virtual int64 Random64();
90
91   struct StorageData {
92     BlockData *block;
93     int pos;
94   };
95
96   static const int kBlockRetry = 100;       // Number of retries to allocate
97                                             // sectors.
98
99   typedef map<int64, StorageData*> AddrToBlockMap;
100   typedef vector<int64> PosToAddrVector;
101   PosToAddrVector pos_to_addr_;
102   AddrToBlockMap addr_to_block_;
103   uint64 nelems_;
104   int sector_size_;          // Sector size, in bytes
105   int write_block_size_;     // Block size, in bytes
106   string device_name_;       // Device name
107   int64 device_sectors_;     // Number of sectors in device
108   int64 segment_size_;       // Segment size, in bytes
109   pthread_mutex_t data_mutex_;
110   pthread_cond_t data_condition_;
111   pthread_mutex_t parameter_mutex_;
112   DISALLOW_COPY_AND_ASSIGN(DiskBlockTable);
113 };
114
115 #endif  // STRESSAPPTEST_BLOCKS_H_