Blame vdo/base/vioPool.c

Packit Service b3514a
/*
Packit Service b3514a
 * Copyright (c) 2020 Red Hat, Inc.
Packit Service b3514a
 *
Packit Service b3514a
 * This program is free software; you can redistribute it and/or
Packit Service b3514a
 * modify it under the terms of the GNU General Public License
Packit Service b3514a
 * as published by the Free Software Foundation; either version 2
Packit Service b3514a
 * of the License, or (at your option) any later version.
Packit Service b3514a
 * 
Packit Service b3514a
 * This program is distributed in the hope that it will be useful,
Packit Service b3514a
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
Packit Service b3514a
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
Packit Service b3514a
 * GNU General Public License for more details.
Packit Service b3514a
 * 
Packit Service b3514a
 * You should have received a copy of the GNU General Public License
Packit Service b3514a
 * along with this program; if not, write to the Free Software
Packit Service b3514a
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
Packit Service b3514a
 * 02110-1301, USA. 
Packit Service b3514a
 *
Packit Service b3514a
 * $Id: //eng/vdo-releases/aluminum/src/c++/vdo/base/vioPool.c#5 $
Packit Service b3514a
 */
Packit Service b3514a
Packit Service b3514a
#include "vioPool.h"
Packit Service b3514a
Packit Service b3514a
#include "logger.h"
Packit Service b3514a
#include "memoryAlloc.h"
Packit Service b3514a
#include "permassert.h"
Packit Service b3514a
Packit Service b3514a
#include "constants.h"
Packit Service b3514a
#include "vio.h"
Packit Service b3514a
#include "types.h"
Packit Service b3514a
Packit Service b3514a
/**
Packit Service b3514a
 * An VIOPool is a collection of preallocated VIOs.
Packit Service b3514a
 **/
Packit Service b3514a
struct vioPool {
Packit Service b3514a
  /** The number of objects managed by the pool */
Packit Service b3514a
  size_t         size;
Packit Service b3514a
  /** The list of objects which are available */
Packit Service b3514a
  RingNode       available;
Packit Service b3514a
  /** The queue of requestors waiting for objects from the pool */
Packit Service b3514a
  WaitQueue      waiting;
Packit Service b3514a
  /** The number of objects currently in use */
Packit Service b3514a
  size_t         busyCount;
Packit Service b3514a
  /** The list of objects which are in use */
Packit Service b3514a
  RingNode       busy;
Packit Service b3514a
  /** The number of requests when no object was available */
Packit Service b3514a
  uint64_t       outageCount;
Packit Service b3514a
  /** The ID of the thread on which this pool may be used */
Packit Service b3514a
  ThreadID       threadID;
Packit Service b3514a
  /** The buffer backing the pool's VIOs */
Packit Service b3514a
  char          *buffer;
Packit Service b3514a
  /** The pool entries */
Packit Service b3514a
  VIOPoolEntry   entries[];
Packit Service b3514a
};
Packit Service b3514a
Packit Service b3514a
/**********************************************************************/
Packit Service b3514a
int makeVIOPool(PhysicalLayer   *layer,
Packit Service b3514a
                size_t           poolSize,
Packit Service b3514a
                ThreadID         threadID,
Packit Service b3514a
                VIOConstructor  *vioConstructor,
Packit Service b3514a
                void            *context,
Packit Service b3514a
                VIOPool        **poolPtr)
Packit Service b3514a
{
Packit Service b3514a
  VIOPool *pool;
Packit Service b3514a
  int result = ALLOCATE_EXTENDED(VIOPool, poolSize, VIOPoolEntry, __func__,
Packit Service b3514a
                                 &pool);
Packit Service b3514a
  if (result != VDO_SUCCESS) {
Packit Service b3514a
    return result;
Packit Service b3514a
  }
Packit Service b3514a
Packit Service b3514a
  pool->threadID = threadID;
Packit Service b3514a
  initializeRing(&pool->available);
Packit Service b3514a
  initializeRing(&pool->busy);
Packit Service b3514a
Packit Service b3514a
  result = ALLOCATE(poolSize * VDO_BLOCK_SIZE, char, "VIO pool buffer",
Packit Service b3514a
                    &pool->buffer);
Packit Service b3514a
  if (result != VDO_SUCCESS) {
Packit Service b3514a
    freeVIOPool(&pool);
Packit Service b3514a
    return result;
Packit Service b3514a
  }
Packit Service b3514a
Packit Service b3514a
  char *ptr = pool->buffer;
Packit Service b3514a
  for (size_t i = 0; i < poolSize; i++) {
Packit Service b3514a
    VIOPoolEntry *entry = &pool->entries[i];
Packit Service b3514a
    entry->buffer       = ptr;
Packit Service b3514a
    entry->context      = context;
Packit Service b3514a
    result = vioConstructor(layer, entry, ptr, &entry->vio);
Packit Service b3514a
    if (result != VDO_SUCCESS) {
Packit Service b3514a
      freeVIOPool(&pool);
Packit Service b3514a
      return result;
Packit Service b3514a
    }
Packit Service b3514a
Packit Service b3514a
    ptr += VDO_BLOCK_SIZE;
Packit Service b3514a
    initializeRing(&entry->node);
Packit Service b3514a
    pushRingNode(&pool->available, &entry->node);
Packit Service b3514a
    pool->size++;
Packit Service b3514a
  }
Packit Service b3514a
Packit Service b3514a
  *poolPtr = pool;
Packit Service b3514a
  return VDO_SUCCESS;
Packit Service b3514a
}
Packit Service b3514a
Packit Service b3514a
/**********************************************************************/
Packit Service b3514a
void freeVIOPool(VIOPool **poolPtr)
Packit Service b3514a
{
Packit Service b3514a
  if (*poolPtr == NULL) {
Packit Service b3514a
    return;
Packit Service b3514a
  }
Packit Service b3514a
Packit Service b3514a
  // Remove all available entries from the object pool.
Packit Service b3514a
  VIOPool *pool = *poolPtr;
Packit Service b3514a
  ASSERT_LOG_ONLY(!hasWaiters(&pool->waiting),
Packit Service b3514a
                  "VIO pool must not have any waiters when being freed");
Packit Service b3514a
  ASSERT_LOG_ONLY((pool->busyCount == 0),
Packit Service b3514a
                  "VIO pool must not have %zu busy entries when being freed",
Packit Service b3514a
                  pool->busyCount);
Packit Service b3514a
  ASSERT_LOG_ONLY(isRingEmpty(&pool->busy),
Packit Service b3514a
                  "VIO pool must not have busy entries when being freed");
Packit Service b3514a
Packit Service b3514a
  VIOPoolEntry *entry;
Packit Service b3514a
  while ((entry = asVIOPoolEntry(chopRingNode(&pool->available))) != NULL) {
Packit Service b3514a
    freeVIO(&entry->vio);
Packit Service b3514a
  }
Packit Service b3514a
Packit Service b3514a
  // Make sure every VIOPoolEntry has been removed.
Packit Service b3514a
  for (size_t i = 0; i < pool->size; i++) {
Packit Service b3514a
    VIOPoolEntry *entry = &pool->entries[i];
Packit Service b3514a
    ASSERT_LOG_ONLY(isRingEmpty(&entry->node), "VIO Pool entry still in use:"
Packit Service b3514a
                    " VIO is in use for physical block %" PRIu64
Packit Service b3514a
                    " for operation %u",
Packit Service b3514a
                    entry->vio->physical,
Packit Service b3514a
                    entry->vio->operation);
Packit Service b3514a
  }
Packit Service b3514a
Packit Service b3514a
  FREE(pool->buffer);
Packit Service b3514a
  FREE(pool);
Packit Service b3514a
  *poolPtr = NULL;
Packit Service b3514a
}
Packit Service b3514a
Packit Service b3514a
/**********************************************************************/
Packit Service b3514a
bool isVIOPoolBusy(VIOPool *pool)
Packit Service b3514a
{
Packit Service b3514a
  return (pool->busyCount != 0);
Packit Service b3514a
}
Packit Service b3514a
Packit Service b3514a
/**********************************************************************/
Packit Service b3514a
int acquireVIOFromPool(VIOPool *pool, Waiter *waiter)
Packit Service b3514a
{
Packit Service b3514a
  ASSERT_LOG_ONLY((pool->threadID == getCallbackThreadID()),
Packit Service b3514a
                  "acquire from active VIOPool called from correct thread");
Packit Service b3514a
Packit Service b3514a
  if (isRingEmpty(&pool->available)) {
Packit Service b3514a
    pool->outageCount++;
Packit Service b3514a
    return enqueueWaiter(&pool->waiting, waiter);
Packit Service b3514a
  }
Packit Service b3514a
Packit Service b3514a
  pool->busyCount++;
Packit Service b3514a
  RingNode *entry = chopRingNode(&pool->available);
Packit Service b3514a
  pushRingNode(&pool->busy, entry);
Packit Service b3514a
  (*waiter->callback)(waiter, entry);
Packit Service b3514a
  return VDO_SUCCESS;
Packit Service b3514a
}
Packit Service b3514a
Packit Service b3514a
/**********************************************************************/
Packit Service b3514a
void returnVIOToPool(VIOPool *pool, VIOPoolEntry *entry)
Packit Service b3514a
{
Packit Service b3514a
  ASSERT_LOG_ONLY((pool->threadID == getCallbackThreadID()),
Packit Service b3514a
                  "vio pool entry returned on same thread as it was acquired");
Packit Service b3514a
  entry->vio->completion.errorHandler = NULL;
Packit Service b3514a
  if (hasWaiters(&pool->waiting)) {
Packit Service b3514a
    notifyNextWaiter(&pool->waiting, NULL, entry);
Packit Service b3514a
    return;
Packit Service b3514a
  }
Packit Service b3514a
Packit Service b3514a
  pushRingNode(&pool->available, &entry->node);
Packit Service b3514a
  --pool->busyCount;
Packit Service b3514a
}
Packit Service b3514a
Packit Service b3514a
/**********************************************************************/
Packit Service b3514a
uint64_t getVIOPoolOutageCount(VIOPool *pool)
Packit Service b3514a
{
Packit Service b3514a
  return pool->outageCount;
Packit Service b3514a
}