1 // Copyright 2008 Google Inc. All Rights Reserved.
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
7 // http://www.apache.org/licenses/LICENSE-2.0
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.
15 // Interface for a thread-safe container of disk blocks
17 #ifndef STRESSAPPTEST_DISK_BLOCKS_H_
18 #define STRESSAPPTEST_DISK_BLOCKS_H_
20 #include <sys/types.h>
28 // This file must work with autoconf on its public version,
29 // so these includes are correct.
32 // Data about a block written to disk so that it can be verified later.
37 void SetParameters(int64 address, int64 size);
38 void IncreaseReferenceCounter();
39 void DecreaseReferenceCounter();
40 int GetReferenceCounter();
41 void SetBlockAsInitialized();
42 bool BlockIsInitialized();
45 void SetPattern(Pattern *p);
46 Pattern *GetPattern();
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
53 pthread_mutex_t data_mutex_;
54 DISALLOW_COPY_AND_ASSIGN(BlockData);
57 // Disk Block table - store data from blocks to be write / read by
59 class DiskBlockTable {
62 virtual ~DiskBlockTable();
64 // Get Number of elements stored on table
66 // Clean all table data
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,
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);
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();
96 static const int kBlockRetry = 100; // Number of retries to allocate
99 typedef map<int64, StorageData*> AddrToBlockMap;
100 typedef vector<int64> PosToAddrVector;
101 PosToAddrVector pos_to_addr_;
102 AddrToBlockMap addr_to_block_;
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);
115 #endif // STRESSAPPTEST_BLOCKS_H_