#!/usr/bin/env python

# Copyright C 2011 by Isilon Systems, Inc. All rights not expressly
# granted herein are reserved.
# Created in the United States of America.
#
# This digital media is protected by U.S. and international copyright and
# intellectual property laws. Unless otherwise specified, all information
# and screens appearing as part of this digital medium, including
# software, services, documents, text, images, icons, and logos design;
# the selection, assembly, arrangement, and design thereof; and the code
# that enables its presentation, are the sole property of Isilon Systems,
# Inc.
#
# Except as otherwise required by applicable law, no part of any
# copyrighted material may be reproduced, modified, translated,
# distributed, published, or transmitted by any means, electronic or
# mechanical, for any purpose, without the prior written consent of Isilon
# Systems.
#
# Isilon and Isilon Systems are registered trademarks of Isilon Systems,
# Inc.  Legion is a trademark of Isilon Systems, Inc.  All other
# trademarks and registered trademarks are the property of their
# respective holders.

import os, sys, isi.misc

MIN_VNODES = 1000
MAX_MEM_PERC = .25
ROUGH_VNODE_SIZE = 2048 # vnodes and associates take up roughly 2k

def get_max_vnodes():
    physmem = int(isi.misc.sysctlbyname('hw.physmem'))

    #Super rough bound
    return int(physmem * MAX_MEM_PERC / ROUGH_VNODE_SIZE)



def main():
    MAX_VNODES = get_max_vnodes()

    if len(sys.argv) != 2 or int(sys.argv[1]) < MIN_VNODES or \
      int(sys.argv[1]) > MAX_VNODES:

        print "Usage: isi_resize_vnode_cache new_maxvnodes"
        print "  maxnodes should be between " + str(MIN_VNODES) + " and " + \
          str(MAX_VNODES)

        sys.exit(1)


    maxvnodes = int(sys.argv[1])

    #As defined by ifs/lock/lk_domains.c:lk_domain_lazy_queue_goal_desiredvnodes
    queuesize = maxvnodes + maxvnodes / 100

    os.system("sysctl kern.maxvnodes=" + str(maxvnodes))
    os.system("sysctl kern.minvnodes=" + str(maxvnodes))
    os.system("sysctl efs.lin.lock.initiator.lazy_queue_goal=" + str(queuesize))
    os.system("sysctl efs.ref.initiator.lazy_queue_goal=" + str(queuesize))

if __name__ == "__main__":
    main()

