To check the fragmentation factor on an XFS filesystem, the XFS-tools are needed. They are normally included in a special package for the distribution. In Debian i.e., this is called xfsprogs, so make sure it is installed.
To check the fragmentation factor, enter:
$ xfs_db -c frag -r /dev/sda3 actual 970007, ideal 38240, fragmentation factor 96.06%
This example shows a high fragmentation factor and a defrag is needed.
To start the defragmentation, enter:
$ time xfs_fsr -t 19000 /dev/sda3 -v
The -v gives a verbose output, which is convenient for seeing that stuff gets done. The -t entries the maximum number of seconds the defrag will proceed. It will probably need several hours to complete.
Note that this can be done on a mounted filesystem, but it's not recommended to defrag during a lot of work load. If the work load is less during nighttime, it might be a good opportunity to execute this.