1 // Copyright 2006 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 // queue.h : simple queue api
16 
17 // This is an interface to a simple thread safe queue,
18 // used to hold data blocks and patterns.
19 // The order in which the blocks are returned is random.
20 
21 #ifndef STRESSAPPTEST_QUEUE_H_  // NOLINT
22 #define STRESSAPPTEST_QUEUE_H_
23 
24 #include <sys/types.h>
25 #include <pthread.h>
26 
27 // This file must work with autoconf on its public version,
28 // so these includes are correct.
29 #include "sattypes.h"  // NOLINT
30 #include "pattern.h"   // NOLINT
31 
32 // Tag indicating no preference.
33 static const int kDontCareTag = -1;
34 // Tag indicating no preference.
35 static const int kInvalidTag = 0xf001;
36 
37 
38 // This describes a block of memory, and the expected fill pattern.
39 struct page_entry {
40   uint64 offset;
41   void *addr;
42   uint64 paddr;
43   class Pattern *pattern;
44   int32 tag;     // These are tags for use in NUMA affinity or other uses.
45   uint32 touch;  // Counter of the number of reads from this page.
46   uint64 ts;     // Timestamp of the last read from this page.
47   class Pattern *lastpattern;  // Expected Pattern at last read.
48 };
49 
50 static inline void init_pe(struct page_entry *pe) {
51   pe->offset = 0;
52   pe->addr = NULL;
53   pe->pattern = NULL;
54   pe->tag = kInvalidTag;
55   pe->touch = 0;
56   pe->ts = 0;
57   pe->lastpattern = NULL;
58 }
59 
60 // This is a threadsafe randomized queue of pages for
61 // worker threads to use.
62 class PageEntryQueue {
63  public:
64   explicit PageEntryQueue(uint64 queuesize);
65   ~PageEntryQueue();
66 
67   // Push a page onto the list.
68   int Push(struct page_entry *pe);
69   // Pop a random page off of the list.
70   int PopRandom(struct page_entry *pe);
71 
72  private:
73   struct page_entry *pages_;  // Where the pages are held.
74   int64 nextin_;
75   int64 nextout_;
76   int64 q_size_;  // Size of the queue.
77   int64 pushed_;  // Number of pages pushed, total.
78   int64 popped_;  // Number of pages popped, total.
79   pthread_mutex_t q_mutex_;
80 
81   DISALLOW_COPY_AND_ASSIGN(PageEntryQueue);
82 };
83 
84 
85 #endif  // MILES_TESTS_SAT_QUEUE_H_ NOLINT
86